You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
| Pattern | Description |
| ------- | :----------------------------------------------------------- | -------------------------------------------- |
| ^ | Matches the start of a string. |
| { | Starts a quantifier for the number of occurrences. |
| + | Matches one or more of the preceding element. |
| < | Not a standard regex meta character (commonly used in HTML). |
| [ | Starts a character class. |
| * | Matches zero or more of the preceding element. |
| ) | Ends a capturing group. |
| > | Not a standard regex meta character (commonly used in HTML). |
| . | Matches any character except a newline. |
| ( | Starts a capturing group. |
| | | Acts as a logical OR within a regex pattern. |
| $ | Matches the end of a string. |
| \ | Escapes a meta character, giving it literal meaning. |
| ? | Matches zero or one of the preceding element. |
{.cols-3 .marker-none}
Escape these special characters with \
Meta Sequences {.row-span-4}
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 either a or b
(?:...)
Match everything enclosed
(?>...)
Atomic group (non-capturing)
(?|...)
Duplicate subpattern group number
(?#...)
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)
Conditional statement
(?(R)yes|no)
Conditional statement
(?(R#)yes|no)
Recursive Conditional statement
(?(R&name\yes|no)
Conditional statement
(?(?=...)yes|no)
Lookahead conditional
(?(?<=...)yes|no)
Lookbehind conditional
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 {.col-span-2}
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{.cols-3}
Characters
Pattern
Matches
ring
Match ring springboard etc.
.
Match a, 9, + etc.
h.o
Match hoo, h2o, h/o etc.
ring\?
Match ring?
\(quiet\)
Match (quiet)
c:\\windows
Match c:\windows
Use \ to search for these special characters: [ \ ^ $ . | ? * + ( ) { }
>>>pet=re.compile(r'dog')
>>> type(pet)
<class'_sre.SRE_Pattern'>>>>bool(pet.search('They bought a dog'))
True>>>bool(pet.search('A cat crossed their path'))
False
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,\slocale dependent
re.S
re.DOTALL
Dot matches all (including newline)
re.U
re.UNICODE
Make \w,\b,\d,\sunicode dependent
re.X
re.VERBOSE
Readable style
Regex in JavaScript
test()
lettextA='I like APPles very much';lettextB='I like APPles';letregex=/apples$/i;// Output: falseconsole.log(regex.test(textA));// Output: trueconsole.log(regex.test(textB));
search()
lettext='I like APPles very much';letregexA=/apples/;letregexB=/apples/i;// Output: -1console.log(text.search(regexA));// Output: 7console.log(text.search(regexB));
exec()
lettext='Do you like apples?';letregex=/apples/;// Output: applesconsole.log(regex.exec(text)[0]);// Output: Do you like apples?console.log(regex.exec(text).input);
match()
lettext='Here are apples and apPleS';letregex=/apples/gi;// Output: [ "apples", "apPleS" ]console.log(text.match(regex));
split() {.col-span-2}
lettext='This 593 string will be brok294en at places where d1gits are.';letregex=/\d+/g;// Output: [ "This ", " string will be brok", "en at places where d", "gits are." ]console.log(text.split(regex));
lettext='Do you like aPPles?';letregex=/apples/i;// Output: Do you like mangoes?letresult=text.replace(regex,'mangoes');console.log(result);
replaceAll()
letregex=/apples/gi;lettext='Here are apples and apPleS';// Output: Here are mangoes and mangoesletresult=text.replaceAll(regex,'mangoes');console.log(result);
Regex in PHP
Functions {.col-span-2}
-
-
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
Stringregex = "[A-Z\n]{5}$";
Stringstr = "I like APP\nLE";
Patternp = Pattern.compile(regex, Pattern.MULTILINE);
Matcherm = p.matcher(str);
// Outputs: I like Apple!System.out.println(m.replaceAll("pple!"));
Starting index of substring matching regex (NOTE: Only MySQL 8.0+)
REGEXP_LIKE()
Whether string matches regex (NOTE: Only MySQL 8.0+)
REGEXP_REPLACE()
Replace substrings matching regex (NOTE: Only MySQL 8.0+)
REGEXP_SUBSTR()
Return substring matching regex (NOTE: Only MySQL 8.0+)
REGEXP
expr REGEXP pat
Examples
mysql>SELECT'abc' REGEXP '^[a-d]';
1
mysql>SELECT name FROM cities WHERE name REGEXP '^A';
mysql>SELECT name FROM cities WHERE name NOT REGEXP '^A';
mysql>SELECT name FROM cities WHERE name REGEXP 'A|B|R';
mysql>SELECT'a' REGEXP 'A', 'a' REGEXP BINARY 'A';
10
REGEXP_REPLACE
REGEXP_REPLACE(expr, pat, repl[, pos[, occurrence[, match_type]]])
Examples
mysql>SELECT REGEXP_REPLACE('a b c', 'b', 'X');
a X c
mysql>SELECT REGEXP_REPLACE('abc ghi', '[a-z]+', 'X', 1, 2);
abc X