0

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?

4
  • "Is there a regex that will make me a valid variable from any string?" No. Commented Dec 2, 2011 at 10:20
  • Ok so then any kind of method? sample? library? i would prefer using something that is tested than creating mine and then resolving any possible issues it might have. Commented Dec 2, 2011 at 10:26
  • 1
    The problem is the "any" in your question. Any can be literally anything. You need to provide more specific rules that cover all, or a large number of your test cases. Then a solution may be feasible. Commented Dec 2, 2011 at 10:36
  • Related to stackoverflow.com/questions/6695624/… the answer might be of interest. Commented Dec 2, 2011 at 12:26

2 Answers 2

2

This will work for your provided samples:

public static String toVar(String str){
    str = str.replaceAll("^[^a-zA-Z_]+", "");
    str = str.replaceAll("[^a-zA-Z_0-9]+", "_");
    str = str.replaceAll("(?<=[a-z])(?=[A-Z])", "_");
    return str.toUpperCase();
}

Sample:

String s;
s = "myJSON";
System.out.println(s + " -> " + toVar(s));

s = "configGlossary:poweredByIcon";
System.out.println(s + " -> " + toVar(s));

s = "124$32SomeSampleString_thatI_have";
System.out.println(s + " -> " + toVar(s));

Output:

myJSON -> MY_JSON
configGlossary:poweredByIcon -> CONFIG_GLOSSARY_POWERED_BY_ICON
124$32SomeSampleString_thatI_have -> SOME_SAMPLE_STRING_THAT_I_HAVE
Sign up to request clarification or add additional context in comments.

1 Comment

My regex in stackoverflow.com/questions/6695624/… could be used if preferred, it handles more details if that's what the OP wants.
1

I have thrown together a quick and (very-)dirty solution in this gist. I'm afraid though, that there is no general solution or library for your problem, as your task is very specific to your problem domain. Hope that helps nonetheless.

The output of the script is the following:

working for: configGlossary:poweredByIcon, got CONFIG_GLOSSARY_POWERED_BY_ICON
working for: 124$32SomeSampleString_thatI_have, got SOME_SAMPLE_STRING_THAT_I_HAVE
working for: myJSP, got MY_JSP

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.