Skip to main content

Regular expressions

Last updated: 5 Aug 2020 ·
Posted in wiki#notes

Character classes

CharacterMeaning
.Any character except newline
\dOne digit
\DOne non-digit
\sOne whitespace
\SOne non-whitespace
\wOne word character
\WOne non-word character
\tHorizontal tab
\vVertical tab
\rCarriage return
\nNewline/Linefeed
\fForm-feed
[\b]Backspace character
\0NUL character
\Escape a special character

Assertions

CharacterMeaning
^Start of string
$End of string
\bWord boundary
\BNon-word boundary
x(?=y)Positive lookahead (Matches "x" only if "x" is followed by "y")
x(?!y)Negative lookahead (Matches "x" only if "x" is not followed by "y")
(?<=y)xPositive lookbehind (Matches "x" only if "x" is preceded by "y")
(?<!y)xNegative lookbehind (Matches "x" only if "x" is not preceded by "y")

Groups and ranges

CharacterMeaning
x|yEither "x" or "y"
[xyz] [a-c]Any one of the enclosed characters
[^xyz] [^a-c]Anything that is not enclosed in the brackets
(x)Capturing group (matches "x" and remembers the match)
(?<Name>x)Named capturing group (matches "x" and stores it as "Name")
(?:x)Non-capturing group (matches "x" but does not remember the match)
\NMatch the Nth captured group
\k<Name>Back reference to the last substring matching the named capturing group specified by "Name"

Quantifiers

CharacterMeaning
*0 or more
+1 or more
?0 or 1
x{n}Exactly n occurences of x
x{n, m}Between n and m occurences of x
x{n,}n or more occurences of x
?Make the quantifier non-greedy (stop as soon as it finds a match)

POSIX classes

CharacterMeaning
[:alnum:]Letters and digits
[:alpha:]Letters
[:ascii:]Ascii codes 0 - 127
[:blank:]Space or tab only
[:cntrl:]Control characters
[:digit:]Decimal digits
[:graph:]Visible characters, except space
[:upper:]Uppercase letters
[:lower:]Lowercase letters
[:word:]Word characters
[:print:]Visible characters
[:punct:]Visible punctuation characters
[:space:]Whitespace
[:xdigit:]Hexadecimal digits