Java Reference · Text, Formatting and Regex
Pattern flags
Reference for java.util.regex.Pattern flags: CASE_INSENSITIVE, MULTILINE, DOTALL, COMMENTS, UNICODE_CASE, UNICODE_CHARACTER_CLASS, LITERAL and inline (?i) forms.
Flags change how a regular expression is interpreted. In Java you set them in two ways: as int constants passed to Pattern.compile(regex, flags) and combined with |, or inside the pattern itself with an inline group such as (?i). Both forms have the same effect; the inline form is the only one available to String.matches, replaceAll and split, which take no flag argument.
Flags are baked into the compiled Pattern. You can read them back with pattern.flags().
Pattern p = Pattern.compile("^error:", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE); boolean same = "ERROR: x".matches("(?i)error: x");
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input · what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input · what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Syntax
Pattern.compile(String regex, int flags)
| Constant | Inline | Effect |
|---|---|---|
CASE_INSENSITIVE |
(?i) |
Ignores case for ASCII letters (all letters when combined with UNICODE_CASE). |
UNICODE_CASE |
(?u) |
Makes case-insensitive matching Unicode-aware. Only has an effect with CASE_INSENSITIVE. |
MULTILINE |
(?m) |
^ and $ match at every line, not only at input start and end. |
DOTALL |
(?s) |
. also matches line terminators. |
COMMENTS |
(?x) |
Ignores whitespace and # comments to end of line in the pattern. |
UNIX_LINES |
(?d) |
Only \n counts as a line terminator for ., ^ and $. |
LITERAL |
none | The pattern is treated as plain text; meta-characters have no meaning. |
UNICODE_CHARACTER_CLASS |
(?U) |
\w, \d, \s, \b and POSIX classes follow Unicode; implies UNICODE_CASE. |
CANON_EQ |
none | Matches canonically equivalent characters (for example e plus combining accent). Slow. |
Inline groups: (?i) applies to the rest of the enclosing group, (?-i) turns a flag off, and (?i:...) applies it to a single sub-expression.
Usage notes
- Combine with bitwise OR:
Pattern.CASE_INSENSITIVE | Pattern.MULTILINE. Using+also works for distinct bits but|is the idiom. MULTILINEis about anchors,DOTALLabout the dot. They are independent; log parsing often needs both.- Unicode surprises. Without
UNICODE_CASE,"é"and"É"are different. WithoutUNICODE_CHARACTER_CLASS,\wdoes not matchï. For letters regardless of flags use\p{L}. LITERALhas no inline form. UsePattern.quote(text)or\Q...\Eto quote part of a pattern.- Flags with
Stringmethods: put(?i)at the beginning of the regex:s.replaceAll("(?i)foo", "bar"). COMMENTSand spaces: in this mode a literal space must be escaped (\) or put in a class ([ ]).- Locale does not affect regex case folding;
Patternuses Unicode rules, not the defaultLocale.