How to make Java regex to replace non-alphabetic signs and add underscores in Java class names?

242 views Asked by At

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

1

There are 1 answers

6
Gelx On BEST ANSWER

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]