Regular ExpressionsRegex modifiers (flags)


Introduction

Regular expression patterns are often used with modifiers (also called flags) that redefine regex behavior. Regex modifiers can be regular (e.g. /abc/i) and inline (or embedded) (e.g. (?i)abc). The most common modifiers are global, case-insensitive, multiline and dotall modifiers. However, regex flavors differ in the number of supported regex modifiers and their types.

Remarks

PCRE Modifiers

ModifierInlineDescription
PCRE_CASELESS(?i)Case insensitive match
PCRE_MULTILINE(?m)Multiple line matching
PCRE_DOTALL(?s). matches new lines
PCRE_ANCHORED(?A)Meta-character ^ matches only at the start
PCRE_EXTENDED(?x)White-spaces are ignored
PCRE_DOLLAR_ENDONLYn/aMeta-character $ matches only at the end
PCRE_EXTRA(?X)Strict escape parsing
PCRE_UTF8Handles UTF-8 characters
PCRE_UTF16Handles UTF-16 characters
PCRE_UTF32Handles UTF-32 characters
PCRE_UNGREEDY(?U)Sets the engine to lazy matching
PCRE_NO_AUTO_CAPTURE(?:)Disables auto-capturing groups

Java Modifiers

Modifier (Pattern.###)ValueDescription
UNIX_LINES1Enables Unix lines mode.
CASE_INSENSITIVE2Enables case-insensitive matching.
COMMENTS4Permits whitespace and comments in a pattern.
MULTILINE8Enables multiline mode.
LITERAL16Enables literal parsing of the pattern.
DOTALL32Enables dotall mode.
UNICODE_CASE64Enables Unicode-aware case folding.
CANON_EQ128Enables canonical equivalence.
UNICODE_CHARACTER_CLASS256Enables the Unicode version of Predefined character classes and POSIX character classes.