Comments on: Case-insensitive replaceAll in Java http://jelaniharris.com/2009/case-insensitive-replaceall-in-java/ The one and only Mon, 25 Sep 2017 10:01:25 +0000 hourly 1 https://wordpress.org/?v=4.8.3 By: Brett http://jelaniharris.com/2009/case-insensitive-replaceall-in-java/comment-page-1/#comment-14470 Fri, 20 Mar 2015 03:48:43 +0000 http://jelaniharris.com/?p=67#comment-14470 The correct phrase is “the quick brown fox jumps over the lazy dog”. Which contains all 26 letters of the English alphabet.

]]>
By: Andrei http://jelaniharris.com/2009/case-insensitive-replaceall-in-java/comment-page-1/#comment-2188 Mon, 05 Aug 2013 08:37:08 +0000 http://jelaniharris.com/?p=67#comment-2188 “The sly brown Fox jumped over the lazy foX.”.replaceAll(“(?i)(fox)”, “[bold]$1[/bold]”);

]]>
By: Paul http://jelaniharris.com/2009/case-insensitive-replaceall-in-java/comment-page-1/#comment-1977 Mon, 25 Feb 2013 17:50:05 +0000 http://jelaniharris.com/?p=67#comment-1977 [bold] … … You must check Matcher & Pattern class to achieve it. You can parse each “match” and replace it

]]>
By: dm http://jelaniharris.com/2009/case-insensitive-replaceall-in-java/comment-page-1/#comment-1913 Fri, 01 Feb 2013 19:27:01 +0000 http://jelaniharris.com/?p=67#comment-1913 What if you want to highlight the matches that were found AND keep the original case? 🙂

eg:

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 http://jelaniharris.com/2009/case-insensitive-replaceall-in-java/comment-page-1/#comment-397 Sun, 06 Nov 2011 16:07:08 +0000 http://jelaniharris.com/?p=67#comment-397 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 http://jelaniharris.com/2009/case-insensitive-replaceall-in-java/comment-page-1/#comment-366 Tue, 26 Jul 2011 09:53:52 +0000 http://jelaniharris.com/?p=67#comment-366 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 http://jelaniharris.com/2009/case-insensitive-replaceall-in-java/comment-page-1/#comment-326 Thu, 19 May 2011 01:32:00 +0000 http://jelaniharris.com/?p=67#comment-326 thank you!

]]>
By: dansesacrale http://jelaniharris.com/2009/case-insensitive-replaceall-in-java/comment-page-1/#comment-94 Tue, 30 Nov 2010 16:50:21 +0000 http://jelaniharris.com/?p=67#comment-94 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 http://jelaniharris.com/2009/case-insensitive-replaceall-in-java/comment-page-1/#comment-90 Mon, 19 Jul 2010 16:57:56 +0000 http://jelaniharris.com/?p=67#comment-90 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 http://jelaniharris.com/2009/case-insensitive-replaceall-in-java/comment-page-1/#comment-89 Sun, 18 Jul 2010 20:05:50 +0000 http://jelaniharris.com/?p=67#comment-89 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);

foi CELSO

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

]]>