Comments on: Case-insensitive replaceAll in Java The one and only Mon, 25 Sep 2017 10:01:25 +0000 hourly 1 By: Brett Fri, 20 Mar 2015 03:48:43 +0000 The correct phrase is “the quick brown fox jumps over the lazy dog”. Which contains all 26 letters of the English alphabet.

By: Andrei Mon, 05 Aug 2013 08:37:08 +0000 “The sly brown Fox jumped over the lazy foX.”.replaceAll(“(?i)(fox)”, “[bold]$1[/bold]”);

By: Paul Mon, 25 Feb 2013 17:50:05 +0000 [bold] … … You must check Matcher & Pattern class to achieve it. You can parse each “match” and replace it

By: dm Fri, 01 Feb 2013 19:27:01 +0000 What if you want to highlight the matches that were found AND keep the original case? 🙂


Input: “The sly brown Fox jumped over the lazy foX.”
Replace Term: fox
Output: “The sly brown [bold]Fox[bold] jumped over the lazy [bold]foX[bold].”

By: tchrist Sun, 06 Nov 2011 16:07:08 +0000 Incredible thought it may seem, Java defaults to only understanding ASCII; it ignores its own native charcter set. So you always need to add “(?u)” to make case insensitivity work on normal Java text, which is Unicode not ASCII.

Also, if you expect things like \w to work on regular Java text, you need to add “(?U)”, which isn’t even supported until Java J.

In short, you normally want “(?iu)” for case insensitivity, and you want “(?U)” for the UNICODE_CHARCLASSES flag.

Note that this is still only the simplistic kind of case insensitivity as provided by the Character class, not full casemappings such as provided by the String class. That will be ok for Spanish and Portuguese, but not for German or Greek.

By: deb Tue, 26 Jul 2011 09:53:52 +0000 Thanks buddy,
it was a great help to me. Searching for something like that
for many hours. Finally ur site helps.

Thanks again, keep up the good work.

By: c9025295 Thu, 19 May 2011 01:32:00 +0000 thank you!

By: dansesacrale Tue, 30 Nov 2010 16:50:21 +0000 Hi, just wanted to let you know that this blog entry came up as the top hit on a Google search “replaceall case insensitive” (without quotes), and I found it helpful and well-written. Thanks.

By: Jelani Harris Mon, 19 Jul 2010 16:57:56 +0000 Hello Celso,

I had a hard time trying to follow what exactly you’re trying to do here so I’ll make a few quick assumptions. I’m assuming that you don’t want to eat the whitespace after the search term.

The regex you have set up now is looking for the term “celso” and one other character that is not a single quote (‘). Because the space character is not a quote the regex matches the term and a single space – then does the replacement. If you were to set the regex to:
String regex = "";
Then you’d be matching all variations of celso without the spaces.

However if you just wanted to match the WORD “celso”, and not part of another word of a mistype (such as “celsOOOOO” which would turn into “OOOO”) you can use the word boundary regex:
String regex = "\\b";

I hope I helped.

By: celso Sun, 18 Jul 2010 20:05:50 +0000 String regex = "[^']"; String insentiveCase = "(?i)"; String DOCUMENTO_SAIDA = "celso foi celso Celso CELSO"; String anotacao = ""; String termo = "celso"; DOCUMENTO_SAIDA = DOCUMENTO_SAIDA.replaceAll(insentiveCase + termo + regex, anotacao); System.out.println(DOCUMENTO_SAIDA);


But… he eats space char after the replaced “termo”