I am trying to convert camel case to space separated values using python. For example:
divLineColor -> div Line Color
This lin
Here's my simple solution, which works with PCRE-like implementations, including Python:
/(?<=[a-zA-Z])(?=[A-Z])/g
Then, simply replace all matches with a single space (). Putting it all together:
re.sub(r'(?<=[a-zA-Z])(?=[A-Z])', ' ', yourCamelCaseString);
SSCCE
Hope this method helps :
public static String convertCamelCaseToStatement(String camelCase) {
StringBuilder builder = new StringBuilder();
for (Character c : camelCase.toCharArray()) {
if (Character.isUpperCase(c)) {
builder.append(" ").append(c);
} else {
builder.append(c);
}
}
return builder.toString();
}
\g<0>
references the matched string of the whole pattern while \g<1>
refereces the matched string of the first subpattern ((…)
). So you should use \g<1>
and \g<2>
instead:
label = re.sub("([a-z])([A-Z])","\g<1> \g<2>",label)
(?<=[a-z])([A-Z])
or
([a-z])([A-Z])