Skip to main content

Regular Expressions

Example Text

Dolorum quaerat animi consequatur labore. Rerum et qui et. Aliquam perferendis dicta libero et voluptatem deleniti. Doloremque cum est aut et et. Repudiandae ad debitis tempora harum. Qui enim est aut dolores consectetur est

Nesciunt porro deleniti ad voluptatem perspiciatis sed. Vel saepe nisi animi sapiente ut nostrum. Iusto est optio commodi suscipit illo minima sint saepe. Vitae magnam quam eos quisquam minima

Anchors

SyntaxDescriptionExample Reg1st match2nd Match
^Start of string or start of line (if m flag)^\w3DolNes
$End of string or start of line (if m flag)\w3$estima
\bWord Boundary\b..Do_q
\BNot Word Boundary\B...olorum
\ZEnd of string\w3\Zima
\zAbsolute end of string\w3\zima

Character Classes

SyntaxDescriptionExample RegMatches
[abd]Character Set
[^abc]Negated Character Set
.Any single character except newline
\wWord (a-z, A-Z, 0-9, also underscore)
\WNot Word
\dDigit
\DNot Digit
\sWhitespace (space, tab, newline)
\SNot Whitespace
\nNewline
\rCarriage Return
\tTab
\0Null Character
\xZZMatches Unicode Hex character ZZ
\x{0025}Matches Unicode Hex character ZZZZ
[:upper:]Upper case letters
[:lower:]Lower case letters
[:alpha:]All letters
[:alnum:]Digits and letters
[:digit:]Digits
[:punct:]Punctuation
[:print:]Printed characters and spaces
[:cntrl:]Control Characters

Quantifiers

SyntaxDescriptionExample RegMatches
*0 or more
+1 or more
?0 or 1
a3Exactly 3 a's
a{3,}3 or more a's
a{3,5}3, 4, or 5 a's
a+?Makes + "lazy", matching as few characters as possible

Groups and Ranges

SyntaxDescriptionExample RegMatches
(ab)Match expression before or after |
(ab)Group multiple tokens together
(?=ABC)Match things BEFORE given value (don't include given value)\w3(?=\senim)Qui
(?<=ABC)Match things AFTER given value (don't include given value)(?<=\senim\s)\w{3}est
a{3,}3 or more a's
a53, 4, or 5 a's
(abc)\4Match 4th subpattern(o.)\1would match olol, but not not present

Pattern Modifiers

SyntaxDescriptionExample RegMatches
/gGlobal (keep matching as much as possible)
/iCase Insensitive match
/sLet . match newlines

Helpful Regex's