Replacement Reference |
Characters |
Matched Text & Backreferences |
Context & Case Conversion |
Conditionals |
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 while in Ruby you cannot turn off this mode at all. 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.
Feature | Syntax | Description | Example | JGsoft | .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. | a matches a and A. | YES | YES | YES | YES | YES | YES | YES | YES | YES | no | no | YES | YES | YES | no | ECMA | YES | no | no | no | no | no | no | no |
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. | test matches test, teST, TEst and TEST. | no | no | no | no | no | no | no | no | no | no | no | no | YES | no | no | no | no | no | no | no | no | no | no | no |
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. | test matches test and teST but not TEst or TEST. | YES | YES | YES | YES | YES | YES | YES | YES | YES | no | no | no | no | YES | no | ECMA | no | no | no | no | no | no | no | no |
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. | YES | YES | YES | YES | YES | YES | YES | YES | YES | no | no | no | 3.6 | YES | no | ECMA | no | no | no | no | no | no | no | no |
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. | test matches test and TEst but not teST or TEST. | YES | YES | YES | YES | YES | YES | YES | YES | YES | n/a | n/a | no | 3.6 | YES | n/a | ECMA | no | n/a | n/a | n/a | n/a | n/a | n/a | n/a |
Reset modifiers | (?^) | Turn off all options. The caret can be followed by modifier letters to turn some options back on. | test matches test and TEst but not teST or TEST. | no | no | no | 5.14 | no | 10.32 | 7.3.0 | no | 4.0.0 | n/a | n/a | no | no | no | n/a | no | no | n/a | n/a | n/a | n/a | n/a | n/a | n/a |
Case insensitive | (?i) | Turn on case insensitivity. | a matches a and A. | YES | YES | YES | YES | YES | YES | YES | YES | YES | n/a | n/a | YES | YES | YES | n/a | ECMA | YES | n/a | n/a | n/a | n/a | n/a | n/a | n/a |
Case sensitive | (?c) | Turn on case sensitivity. | a matches a but not A. | no | no | no | no | no | no | no | no | no | n/a | n/a | no | no | no | n/a | no | YES | n/a | n/a | n/a | n/a | n/a | n/a | n/a |
Free-spacing | (?x) | Turn on free-spacing mode to ignore whitespace between regex tokens and allow # comments. | a#b matches a | YES | YES | YES | YES | YES | YES | YES | YES | YES | n/a | n/a | YES | YES | YES | n/a | ECMA | YES | n/a | n/a | n/a | n/a | n/a | n/a | n/a |
Free-spacing | (?xx) | Turn on free-spacing mode to ignore whitespace between regex tokens and allow # comments, both inside and outside character classes. | [ a] matches a but not | no | no | no | 5.26 | no | 10.30 | 7.3.0 | no | 4.0.0 | n/a | n/a | no | no | no | n/a | no | no | n/a | n/a | n/a | n/a | n/a | n/a | n/a |
Exact spacing | (?t) | Turn on “tight” or exact spacing mode to treat whitespace and # characters as literals. | a#b matches a#b | no | no | no | no | no | no | no | no | no | n/a | n/a | no | no | no | n/a | no | YES | n/a | n/a | n/a | n/a | n/a | n/a | n/a |
Single-line | (?s) | Make the dot match all characters including line break characters. | .* matches ab\n\ndef in ab\n\ndef | YES | YES | YES | YES | YES | YES | YES | YES | YES | n/a | n/a | YES | YES | no | n/a | ECMA | no | n/a | n/a | n/a | n/a | n/a | n/a | n/a |
Multi-line | (?m) | Make ^ and $ match at the start and end of each line. | ^. matches a and d in ab\n\ndef | YES | YES | YES | YES | YES | YES | YES | YES | YES | n/a | n/a | YES | YES | no | n/a | ECMA | no | n/a | n/a | n/a | n/a | n/a | n/a | n/a |
Single-line | (?m) | Make the dot match all characters including line break characters. | .* matches ab\n\ndef in ab\n\ndef | no | no | no | no | no | no | no | no | no | n/a | n/a | no | no | YES | n/a | no | no | n/a | n/a | n/a | n/a | n/a | n/a | n/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. | ^.{3} matches only ab\n in ab\n\ndef | no | no | no | no | no | no | no | no | no | n/a | n/a | no | no | no | n/a | no | YES | n/a | n/a | n/a | n/a | n/a | n/a | n/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. | ^. matches a and d in ab\n\ndef | no | no | no | no | no | no | no | no | no | n/a | n/a | no | no | no | n/a | no | YES | n/a | n/a | n/a | n/a | n/a | n/a | n/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. | ^. matches a and d in ab\n\ndef | no | no | no | no | no | no | no | no | no | n/a | n/a | no | no | no | n/a | no | YES | n/a | n/a | n/a | n/a | n/a | n/a | n/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. | ^.* matches only ab in ab\n\ndef | no | no | no | no | no | no | no | no | no | n/a | n/a | no | no | no | n/a | no | YES | n/a | n/a | n/a | n/a | n/a | n/a | n/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. | ^. matches a, the first \n, and d in ab\n\ndef | no | no | no | no | no | no | no | no | no | n/a | n/a | no | no | no | n/a | no | YES | n/a | n/a | n/a | n/a | n/a | n/a | n/a |
Explicit capture | (?n) | Plain parentheses are non-capturing groups instead of numbered capturing groups. Only named capturing groups actually capture. | (a|b)c is the same as (?:a|b)c | YES | YES | no | 5.22 | no | 10.30 | 7.3.0 | no | 4.0.0 | n/a | n/a | YES | no | no | n/a | no | no | n/a | n/a | n/a | n/a | n/a | n/a | n/a |
Duplicate named groups | (?J) | Allow multiple named capturing groups to share the same name. | (?:(?'x'a)|(?'x'b))\k'x' matches aa or bb | no | no | no | no | 6.7 | YES | 5.2.0 | YES | YES | n/a | n/a | no | no | no | n/a | no | no | n/a | n/a | n/a | n/a | n/a | n/a | n/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. | a* is lazy and a*? is greedy | no | no | no | no | YES | YES | YES | YES | YES | n/a | n/a | no | no | no | n/a | no | no | n/a | n/a | n/a | n/a | n/a | n/a | n/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 | ^. matches a and c in a\rb\nc | no | no | YES | no | no | no | no | no | no | n/a | n/a | no | no | no | n/a | no | no | n/a | n/a | n/a | n/a | n/a | n/a | n/a |
BRE | (?b) | Interpret the regular expression as a POSIX BRE | a\+ matches aaa | no | no | no | no | no | no | no | no | no | n/a | n/a | no | no | no | n/a | no | YES | n/a | n/a | n/a | n/a | n/a | n/a | n/a |
ERE | (?e) | Interpret the regular expression as a POSIX ERE | [a\]+ matches a\a\a | no | no | no | no | no | no | no | no | no | n/a | n/a | no | no | no | n/a | no | YES | n/a | n/a | n/a | n/a | n/a | n/a | n/a |
Literal | (?q) | Interpret the regular expression as a literal string (excluding the modifier) | [a\]+ matches [a\]+ literally | no | no | no | no | no | no | no | no | no | n/a | n/a | no | no | no | n/a | no | YES | n/a | n/a | n/a | n/a | n/a | n/a | n/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. | \q is an error while \q matches q | no | no | no | no | YES | no | 5.0.0–7.2.34 | YES | 2.14.0–3.6.3 | n/a | n/a | no | no | no | n/a | no | no | n/a | n/a | n/a | n/a | n/a | n/a | n/a |
Feature | Syntax | Description | Example | JGsoft | .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 |
| Quick Start | Tutorial | Tools & Languages | Examples | Reference | Book Reviews |
| 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 |
| Characters | Matched Text & Backreferences | Context & Case Conversion | Conditionals |
Page URL: https://www.regular-expressions.info/refmodifiers.html
Page last updated: 13 December 2023
Site last updated: 06 November 2024
Copyright © 2003-2024 Jan Goyvaerts. All rights reserved.