Cheat sheets for RegExp

Cheat sheets for RegExp

#General

Basic Syntax

/.../Start and end regex delimiters
|Alternation
()Grouping

Position Matching

^Start of string or start of line in multi-line mode
\AStart of string
$End of string or end of line in multi-line mode
\ZEnd of string
\bWord boundary
\BNot word boundary
\<Start of word
\>End of word

Character Classes

\sWhitespace
\SNot whitespace
\wWord
\WNot word
\dDigit
\DNot digit
\xHexadecimal digit
\OOctal digit

Special Characters

\nNewline
\rCarriage return
\tTab
\vVertical tab
\fForm feed
\xxxOctal character xxx
\xhhHex character hh

Groups and Ranges

.Any character except newline (\n)
(a|b)a or b
(…)Group
(?:…)Passive (non-capturing) group
[abc]a, b or c
[^abc]Not a, b or c
[a-z]Letters from a to z
[A-Z]Uppercase letters from A to Z
[0-9]Digits from 0 to 9

Note: Ranges are inclusive.

Quantifiers

*0 or more
+1 or more
?0 or 1
{3}Exactly 3
{3,}3 or more
{3,5}3, 4 or 5

Note: Quantifiers are greedy - they match as many times as possible. Add a ? after the quantifier to make it ungreedy.

Escape Sequences

\:Escape following character. Used to escape any of the following metacharacters: {}[]()^$.|*+?\.
\QBegin literal sequence
\EEnd literal sequence

String Replacement

$11st group
$22nd group
$nnth group
`$``Before matched string
$'After matched string
$+Last matched string
$&Entire matched string

Note: Some regex implementations use \ instead of $.

Assertions

?=Lookahead assertion
?!Negative lookahead
?<=Lookbehind assertion
?!=, ?<!Negative lookbehind
?>Once-only subexpression
?()Condition if-then
?()|Condition if-then-else
?#Comment

POSIX

[:upper:]Uppercase letters
[:lower:]Lowercase letters
[:alpha:]All letters
[:alnum:]Digits and letters
[:digit:]Digits
[:xdigit:]Hexadecimal digits
[:punct:]Punctuation
[:blank:]Space and tab
[:space:]Blank characters
[:cntrl:]Control characters
[:graph:]Printed characters
[:print:]Printed characters and spaces
[:word:]Digits, letters and underscore

Pattern Modifiers

gGlobal match
iCase-insensitive
mMulti-line mode. Causes ^ and $ to also match the start/end of lines.
sSingle-line mode. Causes . to match all, including line breaks.
xAllow comments and whitespace in pattern
eEvaluate replacement
UUngreedy mode
Loading...