Is Java RegEx case-insensitive?
Yes, case insensitivity can be enabled and disabled at will in Java regex.
It looks like you want something like this:
System.out.println(
"Have a meRry MErrY Christmas ho Ho hO"
.replaceAll("(?i)\\b(\\w+)(\\s+\\1)+\\b", "$1")
);
// Have a meRry Christmas ho
Note that the embedded Pattern.CASE_INSENSITIVE
flag is (?i)
not \?i
. Note also that one superfluous \b
has been removed from the pattern.
The (?i)
is placed at the beginning of the pattern to enable case-insensitivity. In this particular case, it is not overridden later in the pattern, so in effect the whole pattern is case-insensitive.
It is worth noting that in fact you can limit case-insensitivity to only parts of the whole pattern. Thus, the question of where to put it really depends on the specification (although for this particular problem it doesn't matter since \w
is case-insensitive.
To demonstrate, here's a similar example of collapsing runs of letters like "AaAaaA"
to just "A"
.
System.out.println(
"AaAaaA eeEeeE IiiIi OoooOo uuUuUuu"
.replaceAll("(?i)\\b([A-Z])\\1+\\b", "$1")
); // A e I O u
Now suppose that we specify that the run should only be collapsed only if it starts with an uppercase letter. Then we must put the (?i)
in the appropriate place:
System.out.println(
"AaAaaA eeEeeE IiiIi OoooOo uuUuUuu"
.replaceAll("\\b([A-Z])(?i)\\1+\\b", "$1")
); // A eeEeeE I O uuUuUuu
More generally, you can enable and disable any flag within the pattern as you wish.
See also
java.util.regex.Pattern
- regular-expressions.info/Modifiers
- Specifying Modes Inside The Regular Expression
- Instead of
/regex/i
(Pattern.CASE_INSENSITIVE
in Java), you can do/(?i)regex/
- Instead of
- Turning Modes On and Off for Only Part of The Regular Expression
- You can also do
/first(?i)second(?-i)third/
- You can also do
- Modifier Spans
- You can also do
/first(?i:second)third/
- You can also do
- Specifying Modes Inside The Regular Expression
- regular-expressions.info/Word Boundaries (there's always a
\b
between a\w
and a\s
)
Related questions
- Can you make just part of a regex case-insensitive?
- Case sensitive and insensitive in the same pattern
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"
You can also match case insensitive regexs and make it more readable by using the Pattern.CASE_INSENSITIVE constant like:
Pattern mypattern = Pattern.compile(MYREGEX, Pattern.CASE_INSENSITIVE);
Matcher mymatcher= mypattern.matcher(mystring);