std.regexp is designed to work only with valid UTF strings as input. To validate untrusted input, use std.utf.validate().
In the following guide, pattern[] refers to a regular expression. The attributes[] refers to a string controlling the interpretation of the regular expression. It consists of a sequence of one or more of the following characters:
Attribute | Action |
---|---|
g | global; repeat over the whole input string |
i | case insensitive |
m | treat as multiple lines separated by newlines |
The format[] string has the formatting characters:
Format | Replaced With |
---|---|
$$ | $ |
$& | The matched substring. |
$` | The portion of string that precedes the matched substring. |
$' | The portion of string that follows the matched substring. |
$ n | The nth capture, where n is a single digit 1-9 and $n is not followed by a decimal digit. |
$ nn | The nnth capture, where nn is a two-digit decimal number 01-99. If nnth capture is undefined or more than the number of parenthesized subexpressions, use the empty string instead. |
Any other $ are left as is.
The replacement format can reference the matches using the $&, $$, $', $`, $0 .. $99 notation:
It is the core foundation for adding powerful string pattern matching capabilities to programs like grep, text editors, awk, sed, etc.
n==0 means the matched substring, n>0 means the n'th parenthesized subexpression. if n is larger than the number of parenthesized subexpressions, null is returned.
Format | Description |
---|---|
& | replace with the match |
\n | replace with the nth parenthesized match, n is 1..9 |
\c | replace with char c. |
which prints: 1