Search code examples
javaregexcase-sensitive

Is Java RegEx case-insensitive?


In Java, when doing a replaceAll to look for a regex pattern like:

replaceAll("\\?i\\b(\\w+)\\b(\\s+\\1)+\\b", "$1"); 

(to remove duplicate consecutive case-insensitive words, e.g. Test test), I'm not sure where I put the ?i. I read that it is supposed to be at the beginning, but if I take it out then i catch duplicate consecutive words (e.g. test test), but not case-insensitive words (e.g. Test test). So I thought I could add the ?i in the beginning but that does not seem to get the job done. Any thoughts? Thanks!


Solution

  • RegexBuddy is telling me if you want to include it at the beginning, this is the correct syntax:

    "(?i)\\b(\\w+)\\b(\\s+\\1)+\\b"