Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Case Insensitive variable for String replaceAll(,) method Java

Tags:

java

string

regex

Can anyone help me with creating a regex for variables in java so that the string variable will be considered to be a case insensitive and replace each and every word like Access, access, etc with WINDOWS of any thing like that?

This is the code:

$html=html.replaceAll(label, "WINDOWS");

Notice that label is a string variable.

like image 576
DhruvPatel Avatar asked Jun 28 '12 00:06

DhruvPatel


1 Answers

Just add the "case insensitive" switch to the regex:

html.replaceAll("(?i)"+label, "WINDOWS");

Note: If the label could contain characters with special regex significance, eg if label was ".*", but you want the label treated as plain text (ie not a regex), add regex quotes around the label, either

html.replaceAll("(?i)\\Q" + label + "\\E", "WINDOWS");

or

html.replaceAll("(?i)" + Pattern.quote(label), "WINDOWS");
like image 68
Bohemian Avatar answered Sep 20 '22 15:09

Bohemian