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?

nhahtdh
  • 55,989
  • 15
  • 126
  • 162
DArkO
  • 15,880
  • 12
  • 60
  • 88
  • "Is there a regex that will make me a valid variable from any string?" No. – FailedDev Dec 02 '11 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. – DArkO Dec 02 '11 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. – FailedDev Dec 02 '11 at 10:36
  • Related to http://stackoverflow.com/questions/6695624/use-regex-in-c-sharp-to-insert-spaces-between-concatenatedfilenameslikethis/6696031#6696031 the answer might be of interest. – Qtax Dec 02 '11 at 12:26

2 Answers2

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
Qtax
  • 33,241
  • 9
  • 83
  • 121
  • 1
    My regex in http://stackoverflow.com/questions/6695624/use-regex-in-c-sharp-to-insert-spaces-between-concatenatedfilenameslikethis/6696031#6696031 could be used if preferred, it handles more details if that's what the OP wants. – Qtax Dec 02 '11 at 12:30
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
Matt
  • 17,290
  • 7
  • 57
  • 71