java - Convert string to lower case except all metacharacters -
i want convert a string (basically regex itself) lowercase want exclude metacharacters getting converted.
e.g. :
if
string x = "[\\s\\s]*test message indicator: (.*)[\\s\\s]*"; x = x.tolowercase();
it convert capital s
in \\s
s
change meaning of regex.
i want avoid that.
final regex should [\\s\\s]*test message indicator: (.*)[\\s\\s]*
.
is there way can easily?
you can use placeholder (that know not in input string) swap in , out uppercase predefined character classes of regular expression. example, replace unicode values 00a0
, 00a1
, 00a2
\s
, \d
, , \w
respectively (or whichever char values anticipate never see), lower case string, , back-replace special chars:
string x = "[\\s\\s]*test message indicator: (.*)[\\s\\s]*"; x = x.replaceall("\\\\s", "\u00a0").replaceall("\\\\d","\u00a1").replaceall("\\\\w","\u00a2");; x = x.tolowercase(); x = x.replaceall("\u00a0", "\\\\s").replaceall("\u00a1","\\\\d").replaceall("\u00a2","\\\\w");; system.out.println(x);
granted accounts few common uppercase character classes (and inefficient depending upon context) - need consult the api if regular expressions complex , need account possible uppercase regular expression values.
Comments
Post a Comment