Skip to content
Tutorials Center
My learning

Topics

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().

Java
Pattern p = Pattern.compile("^error:", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
boolean same = "ERROR: x".matches("(?i)error: x");

Try it

Java Case, lines and dots

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.

Java COMMENTS, LITERAL and Unicode

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

Java
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.
  • MULTILINE is about anchors, DOTALL about the dot. They are independent; log parsing often needs both.
  • Unicode surprises. Without UNICODE_CASE, "é" and "É" are different. Without UNICODE_CHARACTER_CLASS, \w does not match ï. For letters regardless of flags use \p{L}.
  • LITERAL has no inline form. Use Pattern.quote(text) or \Q...\E to quote part of a pattern.
  • Flags with String methods: put (?i) at the beginning of the regex: s.replaceAll("(?i)foo", "bar").
  • COMMENTS and spaces: in this mode a literal space must be escaped (\ ) or put in a class ([ ]).
  • Locale does not affect regex case folding; Pattern uses Unicode rules, not the default Locale.