Anchors
^ |
Matches at the start of string or start of line if multi-line mode is enabled. Many regex implementations have multi-line mode enabled by default. |
$ |
Matches at the end of string or end of line if multi-line mode is enabled. Many regex implementations have multi-line mode enabled by default. |
\A |
Matches at the start of the search string. |
\Z |
Matches at the end of the search string, or before a newline at the end of the string. |
\z |
Matches at the end of the search string. |
\b |
Matches at word boundaries. |
\B |
Matches anywhere but word boundaries. |
Character Classes Can be used in ranges
. |
Matches any character except newline. Will also match newline if single-line mode is enabled. |
\s |
Matches white space characters. |
\S |
Matches anything but white space characters. |
\d |
Matches digits. Equivalent to [0-9]. |
\D |
Matches anything but digits. Equivalent to [^0-9]. |
\w |
Matches letters, digits and underscores. Equivalent to [A-Za-z0-9_]. |
\W |
Matches anything but letters, digits and underscores. Equivalent to [^A-Za-z0-9_]. |
\xff |
Matches ASCII hexadecimal character ff. |
\x{ffff} |
Matches UTF-8 hexadecimal character ffff. |
\cA |
Matches ASCII control character ^A. Control characters are case insensitive. |
\132 |
Matches ASCII octal character 132. |
Quantifiers
* |
0 or more. Matches will be as large as possible. |
*? |
0 or more, lazy. Matches will be as small as possible. |
+ |
1 or more. Matches will be as large as possible. |
+? |
1 or more, lazy. Matches will be as small as possible. |
? |
0 or 1. Matches will be as large as possible. |
?? |
0 or 1, lazy. Matches will be as small as possible. |
{2} |
2 exactly. |
{2,} |
2 or more. Matches will be as large as possible. |
{2,}? |
2 or more, lazy. Matches will be as small as possible. |
{2,4} |
2, 3 or 4. Matches will be as large as possible. |
{2,4}? |
2, 3 or 4, lazy. Matches will be as small as possible. |
POSIX Character Classes
Must be used in bracket expressions |
[:upper:] |
Matches uppercase letters. Equivalent to A-Z. |
[:lower:] |
Matches lowercase letters. Equivalent to a-z. |
[:alpha:] |
Matches letters. Equivalent to A-Za-z. |
[:alnum:] |
Matches letters and digits. Equivalent to A-Za-z0-9. |
[:ascii:] |
Matches ASCII characters. Equivalent to \x00-\x7f. |
[:word:] |
Matches letters, digits and underscores. Equivalent to \w. |
[:digit:] |
Matches digits. Equivalent to 0-9. |
[:xdigit:] |
Matches characters that can be used in hexadecimal codes. Equivalent to A-Fa-f0-9. |
[:punct:] |
Matches punctuation. |
[:blank:] |
Matches space and tab. Equivalent to [ \t]. |
[:space:] |
Matches space, tab and newline. Equivalent to \s. |
[:cntrl:] |
Matches control characters. Equivalent to [\x00-\x1F\x7F]. |
[:graph:] |
Matches printed characters. Equivalent to [\x21-\x7E]. |
[:print:] |
Matches printed characters and spaces. Equivalent to [\x21-\x7E ]. |