Back to Cheatsheets

regexp

Others2020-03-10

RegExp

{: .-three-column}

Character classes

PatternDescription
.Any character, except newline
\wWord
\dDigit
\sWhitespace
\WNot word
\DNot digit
\SNot whitespace
[abc]Any of a, b, or c
[a-e]Characters between a and e
[1-9]Digit between 1 and 9
[[:print:]]Any printable character including spaces
[^abc]Any character except a, b or c

Anchors

PatternDescription
\GStart of match
^Start of string *
$End of string *
\AStart of string
\ZEnd of string
\zAbsolute end of string
\bA word boundary
\BNon-word boundary
^abcStart with abc
abc$End with abc

For multiline patterns (m flag), ^ and $ will act as start and end of line.

Escaped characters

PatternDescription
\. \* \\Escape special character used by regex
\tTab
\nNewline
\rCarriage return

Groups

PatternDescription
(abc)Capture group
`(ab)`
(?:abc)Match abc, but don't capture
\1Substituted with text matched of the 1st capturing group

Quantifiers

PatternDescription
a*Match 0 or more
a+Match 1 or more
a?Match 0 or 1
a{5}Match exactly 5
a{,3}Match up to 3
a{3,}Match 3 or more
a{1,3}Match between 1 and 3

Lookahead & Lookbehind

PatternDescription
a(?=b)Match a in baby but not in bay
a(?!b)Match a in Stan but not in Stab
------
(?<=a)bMatch b in crabs but not in cribs
(?<!a)bMatch b in fib but not in fab