Quick Start
Tutorial
Tools & Languages
Examples
Reference
Book Reviews
Regex Reference
Introduction
Table of Contents
Quick Reference
Characters
Basic Features
Character Classes
Shorthands
Anchors
Word Boundaries
Quantifiers
Unicode
Capturing Groups & Backreferences
Named Groups & Backreferences
Special Groups
Mode Modifiers
Recursion & Balancing Groups
Replacement Reference
Characters
Matched Text & Backreferences
Context & Case Conversion
Conditionals
More on This Site
Introduction
Regular Expressions Quick Start
Regular Expressions Tutorial
Replacement Strings Tutorial
Applications and Languages
Regular Expressions Examples
Regular Expressions Reference
Replacement Strings Reference
Book Reviews
Printable PDF
About This Site
RSS Feed & Blog
RegexBuddy—Better than a regular expression reference!

Regular Expression Reference: Mode Modifiers

Mode modifier syntax consists of two elements that differ among regex flavors. Parentheses and a question mark are used to add the modifier to the regex. Depending on its position in the regex and the regex flavor it may affect the whole regex or part of it. If a flavor supports at least one modifier syntax, then it will also support one or more letters that can be used inside the modifier to toggle specific modes. If it doesn’t, “n/a” is indicated for all letters for that flavors.

If a flavor supports mode modifiers but does not support a particular letter, it will be indicated as “no”. That does not mean that the flavor doesn’t have this mode at all. The flavor may still have the mode, but no option to turn it off. Modes are also not necessarily off by default. For example, in most regex flavors, ^ and $ match at the start and end of the string only by default. But the Just Great Software applications and Ruby, they match at the start and end of each line by default. In the JGsoft applications, you can turn off this mode with (?-m) while in Ruby you cannot turn off this mode at all. (?-m) affects the dot rather than the anchors in Ruby.

The table below only indicates whether each flavor supports a particular letter to toggle a particular mode. It does not indicate the defaults.

FeatureSyntaxDescriptionExampleJGsoft .NET Java Perl PCRE PCRE2 PHP Delphi R JavaScript VBScript XRegExp Python Ruby std::regex Boost Tcl ARE POSIX BRE POSIX ERE GNU BRE GNU ERE Oracle XML XPath
Mode modifier (?letters) at the start of the regex A mode modifier at the start of the regex affects the whole regex and overrides any options set outside the regex. (?i)a matches a and A. YESYESYESYESYESYESYESYESYESnonoYESYESYESnoECMAYESnonononononono
Mode modifier (?letters) in the middle of the regex A mode modifier in the middle of the regex affects the whole regex and overrides any options set outside the regex. te(?i)st matches test, teST, TEst and TEST. nonononononononononononoYESnonononononononononono
Mode modifier (?letters) in the middle of the regex A mode modifier in the middle of the regex affects only the part of the regex to the right of the modifier. If the modifier is used inside a group, it only affects the part of the regex inside that group to the right of the modifier. If the regex or group uses alternation, all alternatives to the right of the modifier are affected. te(?i)st matches test and teST but not TEst or TEST. YESYESYESYESYESYESYESYESYESnonononoYESnoECMAnononononononono
Modifier group (?letters:regex) Non-capturing group with modifiers that affect only the part of the regex inside the group. te(?i:st) matches test and teST but not TEst or TEST. YESYESYESYESYESYESYESYESYESnonono3.6YESnoECMAnononononononono
Negative modifier (?on-off) and (?on-off:regex) Modifier letters (if any) before the hyphen are turned on, while modifier letters after the hyphen are turned off. (?i)te(?-i)st matches test and TEst but not teST or TEST. YESYESYESYESYESYESYESYESYESn/an/ano3.6YESn/aECMAnon/an/an/an/an/an/an/a
Reset modifiers (?^) Turn off all options. The caret can be followed by modifier letters to turn some options back on. (?i)te(?^)st matches test and TEst but not teST or TEST. nonono5.14no10.327.3.0no4.0.0n/an/anononon/anonon/an/an/an/an/an/an/a
Case insensitive (?i) Turn on case insensitivity. (?i)a matches a and A. YESYESYESYESYESYESYESYESYESn/an/aYESYESYESn/aECMAYESn/an/an/an/an/an/an/a
Case sensitive (?c) Turn on case sensitivity. (?c)a matches a but not A. nononononononononon/an/anononon/anoYESn/an/an/an/an/an/an/a
Free-spacing (?x) Turn on free-spacing mode to ignore whitespace between regex tokens and allow # comments. (?x)a#b matches a YESYESYESYESYESYESYESYESYESn/an/aYESYESYESn/aECMAYESn/an/an/an/an/an/an/a
Free-spacing (?xx) Turn on free-spacing mode to ignore whitespace between regex tokens and allow # comments, both inside and outside character classes. (?xx)[ a] matches a but not   nonono5.26no10.307.3.0no4.0.0n/an/anononon/anonon/an/an/an/an/an/an/a
Exact spacing (?t) Turn on “tight” or exact spacing mode to treat whitespace and # characters as literals. (?t)a#b matches a#b nononononononononon/an/anononon/anoYESn/an/an/an/an/an/an/a
Single-line (?s) Make the dot match all characters including line break characters. (?s).* matches ab\n\ndef in ab\n\ndef YESYESYESYESYESYESYESYESYESn/an/aYESYESnon/aECMAnon/an/an/an/an/an/an/a
Multi-line (?m) Make ^ and $ match at the start and end of each line. (?m)^. matches a and d in ab\n\ndef YESYESYESYESYESYESYESYESYESn/an/aYESYESnon/aECMAnon/an/an/an/an/an/an/a
Single-line (?m) Make the dot match all characters including line break characters. (?m).* matches ab\n\ndef in ab\n\ndef nononononononononon/an/anonoYESn/anonon/an/an/an/an/an/an/a
Tcl single-line (?s) Make ^ and $ match at the start and end of the string only. Make the dot match all characters including line break characters. (?s)^.{3} matches only ab\n in ab\n\ndef nononononononononon/an/anononon/anoYESn/an/an/an/an/an/an/a
Tcl multi-line (?m) Make ^ and $ match at the start and end of each line. Do not allow the dot and negated character classes to match line break characters. (?m)^. matches a and d in ab\n\ndef nononononononononon/an/anononon/anoYESn/an/an/an/an/an/an/a
Tcl multi-line (?n) Make ^ and $ match at the start and end of each line. Do not allow the dot and negated character classes to match line break characters. (?n)^. matches a and d in ab\n\ndef nononononononononon/an/anononon/anoYESn/an/an/an/an/an/an/a
Tcl “partial” newline-sensitive (?p) Make ^ and $ match at the start and end of the string only. Do not allow the dot and negated character classes to match line break characters. (?p)^.* matches only ab in ab\n\ndef nononononononononon/an/anononon/anoYESn/an/an/an/an/an/an/a
Tcl “weird” newline-sensitive (?w) Make ^ and $ match at the start and end of each line. Make the dot match all characters including line break characters. (?w)^. matches a, the first \n, and d in ab\n\ndef nononononononononon/an/anononon/anoYESn/an/an/an/an/an/an/a
Explicit capture (?n) Plain parentheses are non-capturing groups instead of numbered capturing groups. Only named capturing groups actually capture. (?n)(a|b)c is the same as (?:a|b)c YESYESno5.22no10.307.3.0no4.0.0n/an/aYESnonon/anonon/an/an/an/an/an/an/a
Duplicate named groups (?J) Allow multiple named capturing groups to share the same name. (?J)(?:(?'x'a)|(?'x'b))\k'x' matches aa or bb nononono6.7YES5.2.0YESYESn/an/anononon/anonon/an/an/an/an/an/an/a
Ungreedy quantifiers (?U) Switches the syntax for greedy and lazy quantifiers. Its use is strongly discouraged because it confuses the meaning of the standard quantifier syntax. (?U)a* is lazy and (?U)a*? is greedy nonononoYESYESYESYESYESn/an/anononon/anonon/an/an/an/an/an/an/a
UNIX lines (?d) When anchors match at line breaks and when the dot does not match line breaks, make them recognize only the line feed character as a line break (?dm)^. matches a and c in a\rb\nc nonoYESnonononononon/an/anononon/anonon/an/an/an/an/an/an/a
BRE (?b) Interpret the regular expression as a POSIX BRE (?b)a\+ matches aaa nononononononononon/an/anononon/anoYESn/an/an/an/an/an/an/a
ERE (?e) Interpret the regular expression as a POSIX ERE (?e)[a\]+ matches a\a\a nononononononononon/an/anononon/anoYESn/an/an/an/an/an/an/a
Literal (?q) Interpret the regular expression as a literal string (excluding the modifier) (?q)[a\]+ matches [a\]+ literally nononononononononon/an/anononon/anoYESn/an/an/an/an/an/an/a
Extra syntax (?X) Treat letters that are escaped with a backslash and that don’t form a regex token as an error instead of as a literal. (?X)\q is an error while (?-X)\q matches q nonononoYESno5.0.0–7.2.34YES2.14.0–3.6.3n/an/anononon/anonon/an/an/an/an/an/an/a
FeatureSyntaxDescriptionExampleJGsoft .NET Java Perl PCRE PCRE2 PHP Delphi R JavaScript VBScript XRegExp Python Ruby std::regex Boost Tcl ARE POSIX BRE POSIX ERE GNU BRE GNU ERE Oracle XML XPath