title, date, icon, background, tags, categories, intro
title
date
icon
background
tags
categories
intro
RegEX
2020-11-25 18:28:43
icon-regex
bg-yellow-500
Regular expressions
regexp
pattern
A quick reference guide for regular expressions (regex), including symbols, ranges, grouping, assertions and some sample patterns to get you started.
This cheat sheet was made for [PCRE](https://en.wikipedia.org/wiki/Perl_Compatible_Regular_Expressions ).
Get Started
Introduction
This is a quick cheat sheet to getting started with regular expressions.
Character Classes
Pattern
Description
[abc]
A single character of: a, b or c
[^abc]
A character except: a, b or c
[a-z]
A character in the range: a-z
[^a-z]
A character not in the range: a-z
[0-9]
A digit in the range: 0-9
[a-zA-Z]
A character in the range: a-z or A-Z
[a-zA-Z0-9]
A character in the range: a-z, A-Z or 0-9
Quantifiers
Pattern
Description
a?
Zero or one of a
a*
Zero or more of a
a+
One or more of a
|[0-9]+ | One or more of 0-9|
|a{3} | Exactly 3 of a|
|a{3,} | 3 or more of a|
|a{3,6} | Between 3 and 6 of a|
|a* | Greedy quantifier|
|a*? | Lazy quantifier|
|a*+ | Possessive quantifier|
Common Metacharacters
^
{
+
<
[
*
)
>
.
(
|
$
\
?
{.cols-3 .style-none}
Escape these special characters with \
Meta Sequences
Pattern
Description
.
Any single character
\s
Any whitespace character
\S
Any non-whitespace character
\d
Any digit, Same as [0-9]
\D
Any non-digit, Same as [^0-9]
\w
Any word character
\W
Any non-word character
\X
Any Unicode sequences, linebreaks included
\C
Match one data unit
\R
Unicode newlines
\v
Vertical whitespace character
\V
Negation of \v - anything except newlines and vertical tabs
\h
Horizontal whitespace character
\H
Negation of \h
\K
Reset match
\n
Match nth subpattern
\pX
Unicode property X
\p{...}
Unicode property or script category
\PX
Negation of \pX
\P{...}
Negation of \p
\Q...\E
Quote; treat as literals
\k<name>
Match subpattern name
\k'name'
Match subpattern name
\k{name}
Match subpattern name
\gn
Match nth subpattern
\g{n}
Match nth subpattern
\g<n>
Recurse nth capture group
\g'n'
Recurses nth capture group.
\g{-n}
Match nth relative previous subpattern
\g<+n>
Recurse nth relative upcoming subpattern
\g'+n'
Match nth relative upcoming subpattern
\g'letter'
Recurse named capture group letter
\g{letter}
Match previously-named capture group letter
\g<letter>
Recurses named capture group letter
\xYY
Hex character YY
\x{YYYY}
Hex character YYYY
\ddd
Octal character ddd
\cY
Control character Y
[\b]
Backspace character
\
Makes any character literal
Anchors
Pattern
Description
\G
Start of match
^
Start of string
$
End of string
\A
Start of string
\Z
End of string
\z
Absolute end of string
\b
A word boundary
\B
Non-word boundary
Substitution
Pattern
Description
\0
Complete match contents
\1
Contents in capture group 1
$1
Contents in capture group 1
${foo}
Contents in capture group foo
\x20
Hexadecimal replacement values
\x{06fa}
Hexadecimal replacement values
\t
Tab
\r
Carriage return
\n
Newline
\f
Form-feed
\U
Uppercase Transformation
\L
Lowercase Transformation
\E
Terminate any Transformation
Group Constructs
Pattern
Description
(...)
Capture everything enclosed
`(a
b)`
(?:...)
Match everything enclosed
(?>...)
Atomic group (non-capturing)
`(?
...)`
(?#...)
Comment
|(?'name'...) | Named Capturing Group|
|(?<name>...) | Named Capturing Group|
|(?P<name>...) | Named Capturing Group|
|(?imsxXU) | Inline modifiers|
|(?(DEFINE)...) | Pre-define patterns before using them|
Assertions
-
-
`(?(1)yes
no)`
`(?(R)yes
no)`
`(?(R#)yes
no)`
`(?(R&name)yes
no)`
`(?(?=...)yes
no)`
`(?(?<=...)yes
no)`
Lookarounds
-
-
(?=...)
Positive Lookahead
(?!...)
Negative Lookahead
(?<=...)
Positive Lookbehind
(?<!...)
Negative Lookbehind
Lookaround lets you match a group before (lookbehind) or after (lookahead) your main pattern without including it in the result.
Flags/Modifiers
Pattern
Description
g
Global
m
Multiline
i
Case insensitive
x
Ignore whitespace
s
Single line
u
Unicode
X
eXtended
U
Ungreedy
A
Anchor
J
Duplicate group names
Recurse
-
-
(?R)
Recurse entire pattern
(?1)
Recurse first subpattern
(?+1)
Recurse first relative subpattern
(?&name)
Recurse subpattern name
(?P=name)
Match subpattern name
(?P>name)
Recurse subpattern name
POSIX Character Classes
Character Class
Same as
Meaning
[[:alnum:]]
[0-9A-Za-z]
Letters and digits
[[:alpha:]]
[A-Za-z]
Letters
[[:ascii:]]
[\x00-\x7F]
ASCII codes 0-127
[[:blank:]]
[\t ]
Space or tab only
[[:cntrl:]]
[\x00-\x1F\x7F]
Control characters
[[:digit:]]
[0-9]
Decimal digits
[[:graph:]]
[[:alnum:][:punct:]]
Visible characters (not space)
[[:lower:]]
[a-z]
Lowercase letters
[[:print:]]
[ -~] == [ [:graph:]]
Visible characters
[[:punct:]]
[!"#$%&’ ()*+,-./:;<=>?@[]^_`{|}~]
Visible punctuation characters
[[:space:]]
[\t\n\v\f\r ]
Whitespace
[[:upper:]]
[A-Z]
Uppercase letters
[[:word:]]
[0-9A-Za-z_]
Word characters
[[:xdigit:]]
[0-9A-Fa-f]
Hexadecimal digits
[[:<:]]
[\b(?=\w)]
Start of word
[[:>:]]
[\b(?<=\w)]
End of word
{.show-header}
Control verb
-
-
(*ACCEPT)
Control verb
(*FAIL)
Control verb
(*MARK:NAME)
Control verb
(*COMMIT)
Control verb
(*PRUNE)
Control verb
(*SKIP)
Control verb
(*THEN)
Control verb
(*UTF)
Pattern modifier
(*UTF8)
Pattern modifier
(*UTF16)
Pattern modifier
(*UTF32)
Pattern modifier
(*UCP)
Pattern modifier
(*CR)
Line break modifier
(*LF)
Line break modifier
(*CRLF)
Line break modifier
(*ANYCRLF)
Line break modifier
(*ANY)
Line break modifier
\R
Line break modifier
(*BSR_ANYCRLF)
Line break modifier
(*BSR_UNICODE)
Line break modifier
(*LIMIT_MATCH=x)
Regex engine modifier
(*LIMIT_RECURSION=d)
Regex engine modifier
(*NO_AUTO_POSSESS)
Regex engine modifier
(*NO_START_OPT)
Regex engine modifier
Regex examples
Characters
Pattern
Matches
ring
Match ring springboard etc.
.
Match a, 9, + etc.
h.o
Match hoo, h2o, h/o etc.
ring\?
Match ing?
\(quiet\)
Match quiet)
c:\\windows
Match c:\windows
Use \ to search for these special characters: [ \ ^ $ . | ? * + ( ) { }
Alternatives
Pattern
Matches
`cat
dog `
`id
identity `
`identity
id `
Order longer to shorter when alternatives overlap
Character classes
Pattern
Matches
[aeiou]
Match any vowel
[^aeiou]
Match a NON vowel
r[iau]ng
Match ring, wrangle, sprung, etc.
gr[ae]y
Match gray or grey
[a-zA-Z0-9]
Match any letter or digit
In [ ] always escape . \ ] and sometimes ^ - .
Shorthand classes
Pattern
Meaning
\w
"Word" character (letter, digit, or underscore)
\d
Digit
\s
Whitespace (space, tab, vtab, newline)
\W, \D, or \S
Not word, digit, or whitespace
[\D\S]
Means not digit or whitespace, both match
[^\d\s]
Disallow digit and whitespace
Occurrences
Pattern
Matches
colou?r
match color or colour
[BW]ill[ieamy's]*
match Bill, Willy, William's etc.
[a-zA-Z]+
match 1 or more letters
\d{3}-\d{2}-\d{4}
match a SSN
[a-z]\w{1,7}
match a UW NetID
Greedy versus lazy
Pattern
Meaning
* + {n,}greedy
match as much as possible
<.+>
finds 1 big match in <b>bold</b>
*? +? {n,}?lazy
match as little as possible
<.+?>
finds 2 matches in <b>bold</b>
Scope
Pattern
Meaning
\b
"Word" edge (next to non "word" character)
\bring
Word starts with "ring", ex ringtone
ring\b
Word ends with "ring", ex spring
\b9\b
Match single digit 9, not 19, 91, 99, etc..
\b[a-zA-Z]{6}\b
Match 6-letter words
\B
Not word edge
\Bring\B
Match springs and wringer
^\d*$
Entire string must be digits
^[a-zA-Z]{4,20}$
String must have 4-20 letters
^[A-Z]
String must begin with capital letter
[\.!?"')]$
String must end with terminal puncutation
Modifiers
Pattern
Meaning
(?i)[a-z]*(?-i)
Ignore case ON / OFF
(?s).*(?-s)
Match multiple lines (causes . to match newline)
(?m)^.*;$(?-m)
^ & $ match lines not whole string
(?x)
#free-spacing mode, this EOL comment ignored
(?-x)
free-spacing mode OFF
/regex/ismx
Modify mode for entire string
Groups
Pattern
Meaning
(in|out)put
Match input or output
\d{5}(-\d{4})?
US zip code ("+ 4" optional)
Parser tries EACH alternative if match fails after group.
Can lead to catastrophic backtracking.
Back references
Pattern
Matches
(to) (be) or not \1 \2
match to be or not to be
([^\s])\1{2}
match non-space, then same twice more aaa, ...
\b(\w+)\s+\1\b
match doubled words
Non-capturing group
Pattern
Meaning
on(?:click|load)
Faster than: on(click|load)
Use non-capturing or atomic groups when possible
Atomic groups
Pattern
Meaning
(?>red|green|blue)
faster than non-capturing
(?>id|identity)\b
match id, but not identity
"id" matches, but \b fails after atomic group,
parser doesn't backtrack into group to retry 'identity'
If alternatives overlap, order longer to shorter.
Lookaround
Pattern
Meaning
(?= )
Lookahead, if you can find ahead
(?! )
Lookahead,if you can not find ahead
(?<= )
Lookbehind, if you can find behind
(?<! )
Lookbehind, if you can NOT find behind
\b\w+?(?=ing\b)
Match warbling, string, fishing, ...
\b(?!\w+ing\b)\w+\b
Words NOT ending in "ing"
(?<=\bpre).*?\b
Match pretend, present, prefix, ...
\b\w{3}(?<!pre)\w*?\b
Words NOT starting with "pre"
\b\w+(?<!ing)\b
Match words NOT ending in "ing"
If-then-else
Match "Mr." or "Ms." if word "her" is later in string
requires lookaround for IF condition
RegEx in Python
Getting started
Import the regular expressions module
Examples
re.search()
re.findall()
re.finditer()
re.split()
re.sub()
re.compile()
Functions
Function
Description
re.findall
Returns a list containing all matches
re.finditer
Return an iterable of match objects (one for each match)
re.search
Returns a Match object if there is a match anywhere in the string
re.split
Returns a list where the string has been split at each match
re.sub
Replaces one or many matches with a string
re.compile
Compile a regular expression pattern for later use
re.escape
Return string with all non-alphanumerics backslashed
Flags
-
-
-
re.I
re.IGNORECASE
Ignore case
re.M
re.MULTILINE
Multiline
re.L
re.LOCALE
Make \w,\b,\s locale dependent
re.S
re.DOTALL
Dot matches all (including newline)
re.U
re.UNICODE
Make \w,\b,\d,\s unicode dependent
re.X
re.VERBOSE
Readable style
Regex in JavaScript
test()
search()
exec()
match()
split()
matchAll()
replace()
replaceAll()
Regex in PHP
Functions
-
-
preg_match()
performs a regex match
preg_match_all()
perform a global regular expression match
preg_replace_callback()
perform a regular expression search and replace using a callback
preg_replace()
perform a regular expression search and replace
preg_split()
splits a string by regex pattern
preg_grep()
returns array entries that match a pattern
preg_replace
preg_match
preg_matchall
preg_grep
preg_split
Regex in Java
Styles
First way
Second way
Third way
Pattern Fields
-
-
CANON_EQ
Canonical equivalence
CASE_INSENSITIVE
Case-insensitive matching
COMMENTS
Permits whitespace and comments
DOTALL
Dotall mode
MULTILINE
Multiline mode
UNICODE_CASE
Unicode-aware case folding
UNIX_LINES
Unix lines mode
Methods
Pattern
Pattern compile(String regex [, int flags])
boolean matches([String regex, ] CharSequence input)
String[] split(String regex [, int limit])
String quote(String s)
Matcher
int start([int group | String name])
int end([int group | String name])
boolean find([int start])
String group([int group | String name])
Matcher reset()
String
boolean matches(String regex)
String replaceAll(String regex, String replacement)
String[] split(String regex[, int limit])
There are more methods ...
Examples
Replace sentence:
Array of all matches: