RegEx


Basics:

Characters:

Character Legend Example Sample Match
\d Most engines: one digit
from 0 to 9
file_\d\d file_25
\w Most engines: "word character": ASCII letter, digit or underscore \w-\w\w\w A-b_1
\s Most engines: "whitespace character": space, tab, newline, carriage return, vertical tab a\sb\sc a b
c
\D One character that is not a digit as defined by your engine's \d \D\D\D ABC
\W One character that is not a word character as defined by your engine's \w \W\W\W\W\W *-+=)
\S One character that is not a whitespace character as defined by your engine's \s \S\S\S\S Yoyo
\ Escapes a special character \.\*\+\?    \$\^\/\\ .*+?    $^/\


Quantifiers:

Quantifier Legend Example Sample Match
+ One or more Version \w-\w+ Version A-b1_1
{3} Exactly three times \D{3} ABC
{2,4} Two to four times \d{2,4} 156
{3,} Three or more times \w{3,} regex_tutorial
* Zero or more times A*B*C* AAACC
? Once or none plurals? plural
+ The + (one or more) is "greedy" \d+ 12345
? Makes quantifiers "lazy" \d+? 1 in 12345
* The * (zero or more) is "greedy" A* AAA
? Makes quantifiers "lazy" A*? empty in AAA
{2,4} Two to four times, "greedy" \w{2,4} abcd
? Makes quantifiers "lazy" \w{2,4}? ab in abcd


Logic:

Logic Legend Example Sample Match
| Alternation / OR operand 22|33 33
( … ) Capturing group A(nt|pple) Apple (captures "pple")
\1 Contents of Group 1 r(\w)g\1x regex
\2 Contents of Group 2 (\d\d)\+(\d\d)=\2\+\1 12+65=65+12
(?: … ) Non-capturing group A(?:nt|pple) Apple

 

White Space:

Character Legend Example Sample Match
\t Tab T\t\w{2} T     ab
\r Carriage return character see below
\n Line feed character see below
\r\n Line separator on Windows AB\r\nCD AB
CD


Character class:

[ … ] One of the characters in the brackets T[ao]p Tap or Top
- Range indicator [a-z] One lowercase letter
[x-y] One of the characters in the range from x to y [A-Z]+ GREAT
[^x] One character that is not x [^a-z]{3} A1!
[^x-y] One of the characters not in the range from x to y


Anchors and Boundaries:

Anchor Legend Example Sample Match
^ Start of string or start of line depending on multiline mode. (But when [^inside brackets], it means "not") ^abc .* abc (line start)
$ End of string or end of line depending on multiline mode. Many engine-dependent subtleties. .*? the end$ this is the end
\A Beginning of string
(all major engines except JS)
\Aabc[\d\D]* abc (string...
...start)
\z Very end of the string
Not available in Python and JS
the end\z
\b Word boundary
Most engines: position where one side only is an ASCII letter, digit or underscore
Bob.*\bcat\b Bob ate the cat


Inline Modifier:

 

Modifier Legend Example Sample Match
(?i) Case-insensitive mode
(except JavaScript)
(?i)Monday monDAY