Tutorial by Examples

A regex pattern where a DOTALL modifier (in most regex flavors expressed with s) changes the behavior of . enabling it to match a newline (LF) symbol: /cat (.*?) dog/s This Perl-style regex will match a string like "cat fled from\na dog" capturing "fled from\na" into Group 1....
Another example is a MULTILINE modifier (usually expressed with m flag (not in Oniguruma (e.g. Ruby) that uses m to denote a DOTALL modifier)) that makes ^ and $ anchors match the start/end of a line, not the start/end of the whole string. /^My Line \d+$/gm will find all lines that start with My...
The common modifier to ignore case is i: /fog/i will match Fog, foG, etc. The inline version of the modifier looks like (?i). Notes: In Java, by default, case-insensitive matching assumes that only characters in the US-ASCII charset are being matched. Unicode-aware case-insensitive matching c...
The modifier that allows using whitespace inside some parts of the pattern to format it for better readability and to allow comments starting with #: /(?x)^ # start of string (?=\D*\d) # the string should contain at least 1 digit (?!\d+$) # the string cannot consist of digi...
This is a .NET regex specific modifier expressed with n. When used, unnamed groups (like (\d+)) are not captured. Only valid captures are explicitly named groups (e.g. (?<name> subexpression)). (?n)(\d+)-(\w+)-(?<id>\w+) will match the whole 123-1_abc-00098, but (\d+) and (\w+) won't...
The UNICODE modifier, usually expressed as u (PHP, Python) or U (Java), makes the regex engine treat the pattern and the input string as Unicode strings and patterns, make the pattern shorthand classes like \w, \d, \s, etc. Unicode-aware. /\A\p{L}+\z/u is a PHP regex to match strings that consis...
The PCRE-compliant PCRE_DOLLAR_ENDONLY modifier that makes the $ anchor match at the very end of the string (excluding the position before the final newline in the string). /^\d+$/D is equal to /^\d+\z/ and matches a whole string that consists of 1 or more digits and will not match "123...
Another PCRE-compliant modifier expressed with /A modifier. If this modifier is set, the pattern is forced to be "anchored", that is, it is constrained to match only at the start of the string which is being searched (the "subject string"). This effect can also be achieved by ap...
The PCRE-compliant PCRE_UNGREEDY flag expressed with /U. It switches greediness inside a pattern: /a.*?b/U = /a.*b/ and vice versa.
One more PCRE modifier that allows the use of duplicate named groups. NOTE: only inline version is supported - (?J), and must be placed at the start of the pattern. If you use /(?J)\w+-(?:new-(?<val>\w+)|\d+-empty-(?<val>[^-]+)-collection)/ the "val" group values will be ...
A PCRE modifier that causes an error if any backslash in a pattern is followed by a letter that has no special meaning. By default, a backslash followed by a letter with no special meaning is treated as a literal. E.g. /big\y/ will match bigy, but /big\y/X will throw an exception. Inline v...

Page 1 of 1