0

I am generating Java classes from input which represents some model language. Customer who creates input models, doesn´t care about Java, so sometimes I get this kind of names of classes:

1) Struct+
2) Controller:
3) *

Obviously, this is not compilable. I would need some nice regex for this to generate something like this:

1) Struct_more
2) Controller_prefix
3) All

Could you please help me to sort out regex for this input?

I want to underscore in between and replace non-alphabetic signs with something meaningfull and valid. Thanks

MartinC
  • 55
  • 8

1 Answers1

2

Well a regex obviously can't replace something on its own. You'd need something to match the regex-meaning with a descriptive string. But a simple regex to match any non alphanumerical character would be somthing like this:

[^A-Za-z0-9]
Gelx
  • 129
  • 7
  • I guess, I could start with something like this: `String className = "Struct+"; String pattern = "[^A-Za-z0-9]"; System.out.println(className.replaceAll(pattern, "_nonalpha"));` – MartinC Nov 08 '14 at 23:14
  • Sure, that would make them valid class-names. But it would not really be descriptive of what the original regex operator was. For example "Struct+" would give you "Struct_nonalpha" "Struct*" would give you the same, if thats not a problem, you have found an easy solution ;) – Gelx Nov 08 '14 at 23:18
  • Yes, I agree with you about the limits. I will talk to customer about this and try to enhance it eventually. Thank you anyway :) – MartinC Nov 08 '14 at 23:20
  • You could also check for every possible operator you expect to find. So you might have `className.replaceAll("\\*", "_all")` to replace all asterisks with "_all". ("\\" to escape the * and not interpret it as regex). You would have to do this for every character though. – Gelx Nov 08 '14 at 23:24
  • That´s true, I will try it. So far, I´ve found only those three, +, : and * – MartinC Nov 08 '14 at 23:26
  • You could maybe mix the two. Replace all known ones with their according string representation and everything else with generic "_nonalpha". You'll have to see. Good luck ;) – Gelx Nov 08 '14 at 23:28