IfCondition.
Syntax and general information
The general usage guideline is to keep regex complexity on the side of simplicity, as its capabilities reside in purely character-level manipulation. As such it's ill-suited for tasks involving higher level invariants like matching an integer number bounded in an [a,b] interval. Checks of this sort of are better addressed by additional post-processing.
The basic syntax shouldn't surprise experienced users of regular expressions. For an introduction to std.regex see a
and its abilities.
There are other web resources on regular expressions to help newcomers, and a good reference with tutorial can easily be found.
This library uses a remarkably common ECMAScript syntax flavor with the following extensions:
- Named subexpressions, with Python syntax.
- Unicode properties such as Scripts, Blocks and common binary properties e.g Alphabetic, White_Space, Hex_Digit etc.
- Arbitrary length and complexity lookbehind, including lookahead in lookbehind and vise-versa.
Pattern syntax
std.regex operates on codepoint level,
'character' in this table denotes a single Unicode codepoint.
| Pattern element | Semantics |
| Atoms | Match single characters |
| any character except [{|*+?()^$ | Matches the character itself. |
| . | In single line mode matches any character.
Otherwise it matches any character except '\n' and '\r'. |
| [class] | Matches a single character
that belongs to this character class. |
| [^class] | Matches a single character that
does not belong to this character class. |
| \cC | Matches the control character corresponding to letter C |
| \xXX | Matches a character with hexadecimal value of XX. |
| \uXXXX | Matches a character with hexadecimal value of XXXX. |
| \U00YYYYYY | Matches a character with hexadecimal value of YYYYYY. |
| \f | Matches a formfeed character. |
| \n | Matches a linefeed character. |
| \r | Matches a carriage return character. |
| \t | Matches a tab character. |
| \v | Matches a vertical tab character. |
| \d | Matches any Unicode digit. |
| \D | Matches any character except Unicode digits. |
| \w | Matches any word character (note: this includes numbers). |
| \W | Matches any non-word character. |
| \s | Matches whitespace, same as \p{White_Space}. |
| \S | Matches any character except those recognized as \s . |
| \\\\ | Matches \ character. |
| \c where c is one of [|*+?() | Matches the character c itself. |
| \p{PropertyName} | Matches a character that belongs
to the Unicode PropertyName set. Single letter abbreviations can be used without surrounding {, }. |
| \P{PropertyName} | Matches a character that does not belong
to the Unicode PropertyName set. Single letter abbreviations can be used without surrounding {, }. |
| \p{InBasicLatin} | Matches any character that is part of
the BasicLatin Unicode block. |
| \P{InBasicLatin} | Matches any character except ones in
the BasicLatin Unicode block. |
| \p{Cyrillic} | Matches any character that is part of
Cyrillic script. |
| \P{Cyrillic} | Matches any character except ones in
Cyrillic script. |
| Quantifiers | Specify repetition of other elements |
| * | Matches previous character/subexpression 0 or more times.
Greedy version - tries as many times as possible. |
| *? | Matches previous character/subexpression 0 or more times.
Lazy version - stops as early as possible. |
| + | Matches previous character/subexpression 1 or more times.
Greedy version - tries as many times as possible. |
| +? | Matches previous character/subexpression 1 or more times.
Lazy version - stops as early as possible. |
| ? | Matches previous character/subexpression 0 or 1 time.
Greedy version - tries as many times as possible. |
| ?? | Matches previous character/subexpression 0 or 1 time.
Lazy version - stops as early as possible. |
| {n} | Matches previous character/subexpression exactly n times. |
| {n,} | Matches previous character/subexpression n times or more.
Greedy version - tries as many times as possible. |
| {n,}? | Matches previous character/subexpression n times or more.
Lazy version - stops as early as possible. |
| {n,m} | Matches previous character/subexpression n to m times.
Greedy version - tries as many times as possible, but no more than m times. |
| {n,m}? | Matches previous character/subexpression n to m times.
Lazy version - stops as early as possible, but no less then n times. |
| Other | Subexpressions & alternations |
| (regex) | Matches subexpression regex, saving matched portion of text for later retrieval. |
| (?#comment) | An inline comment that is ignored while matching. |
| (?:regex) | Matches subexpression regex, not saving matched portion of text. Useful to speed up matching. |
| A|B | Matches subexpression A, or failing that, matches B. |
| (?P<name≥regex) | Matches named subexpression
regex labeling it with name 'name'. When referring to a matched portion of text, names work like aliases in addition to direct numbers. |
| Assertions | Match position rather than character |
| ^ | Matches at the beginning of input or line (in multiline mode). |
| $ | Matches at the end of input or line (in multiline mode). |
| \b | Matches at word boundary. |
| \B | Matches when not at word boundary. |
| (?=regex) | Zero-width lookahead assertion.
Matches at a point where the subexpression regex could be matched starting from the current position. |
| (?!regex) | Zero-width negative lookahead assertion.
Matches at a point where the subexpression regex could not be matched starting from the current position. |
| (?<=regex) | Zero-width lookbehind assertion. Matches at a point
where the subexpression regex could be matched ending at the current position (matching goes backwards). |
| (?<!regex) | Zero-width negative lookbehind assertion.
Matches at a point where the subexpression regex could not be matched ending at the current position (matching goes backwards). |
Character classes
| Pattern element | Semantics |
| Any atom | Has the same meaning as outside of a character class, except for ] which must be written as \\] |
| a-z | Includes characters a, b, c, ..., z. |
| [a||b], [a--b], [a~~b], [a&&b] | Where a, b are arbitrary classes, means union, set difference, symmetric set difference, and intersection respectively. Any sequence of character class elements implicitly forms a union. |
Regex flags
| Flag | Semantics |
| g | Global regex, repeat over the whole input. |
| i | Case insensitive matching. |
| m | Multi-line mode, match ^, $ on start and end line separators
as well as start and end of input. |
| s | Single-line mode, makes . match '\n' and '\r' as well. |
| x | Free-form syntax, ignores whitespace in pattern, useful for formatting complex regular expressions. |
Unicode support
This library provides full Level 1 support* according to
UTS 18. Specifically:- 1.1 Hex notation via any of \uxxxx, \U00YYYYYY, \xZZ.
- 1.2 Unicode properties.
- 1.3 Character classes with set operations.
- 1.4 Word boundaries use the full set of "word" characters.
- 1.5 Using simple casefolding to match case
insensitively across the full range of codepoints.
- 1.6 Respecting line breaks as any of
\u000A | \u000B | \u000C | \u000D | \u0085 | \u2028 | \u2029 | \u000D\u000A.
- 1.7 Operating on codepoint level.
*With exception of point 1.1.1, as of yet, normalization of input is expected to be enforced by user.
Replace format string
A set of functions in this module that do the substitution rely on a simple format to guide the process. In particular the table below applies to the format argument of
The format string can reference parts of match using the following notation.
| Format specifier | Replaced by |
| $& | the whole match. |
| $` | part of input preceding the match. |
| $' | part of input following the match. |
| $$ | '$' character. |
| \c , where c is any character | the character c itself. |
| \\\\ | '\\' character. |
| $1 .. $99 | submatch number 1 to 99 respectively. |
Slicing and zero memory allocations orientation
All matches returned by pattern matching functionality in this library are slices of the original input. The notable exception is the replace family of functions that generate a new string from the input.
In cases where producing the replacement is the ultimate goal
replaceFirstInto and replaceAllInto could come in handyas functions that avoid allocations even for replacement.