Hello i am trying to modify any string to a valid variable made with Uppercase Underscore.
So for example if i have the following:
configGlossary:poweredByIcon -> CONFIG_GLOSSARY_POWERED_BY_ICON
124$32SomeSampleString_thatI_have -> SOME_SAMPLE_STRING_THAT_I_HAVE
myJSP -> MY_JSP but my regex produces MY_J_S_P
First i am using a function to create the Uppercase underscore from camelCase string.
so far i have been able to achieve the required result for everything except the last result example and also the case when there are numbers at start of the string. i have with the following regex. This will remove also the multiple underscores if any.
String regex = "s/^[^a-zA-Z_]+|[^a-zA-Z_0-9]";
String result = variableName.replaceAll(regex, "_").replaceAll("\\_+", "_");
so my question is how do produce the required result. is there a regex that will make me a valid variable from any string?
And is there a way to solve the last sample?