This is a quick cheat sheet to getting started with regular expressions. ... re.compile, Compile a regular expression pattern for later use.
RegEXcheatsheetAquickreferenceforregularexpressions(regex),includingsymbols,ranges,grouping,assertionsandsomesamplepatternstogetyoustarted.
#GettingStarted
#Introduction
Thisisaquickcheatsheettogettingstartedwithregularexpressions.
RegexinPython(quickref.me)
RegexinJavaScript(quickref.me)
RegexinPHP(quickref.me)
RegexinJava(quickref.me)
RegexinMySQL(quickref.me)
RegexinVim(quickref.me)
RegexinEmacs(quickref.me)
Onlineregextester(regex101.com)
#CharacterClasses
Pattern
Description
[abc]
Asinglecharacterof:a,borc
[^abc]
Acharacterexcept:a,borc
[a-z]
Acharacterintherange:a-z
[^a-z]
Acharacternotintherange:a-z
[0-9]
Adigitintherange:0-9
[a-zA-Z]
Acharacterintherange:a-zorA-Z
[a-zA-Z0-9]
Acharacterintherange:a-z,A-Zor0-9
#Quantifiers
Pattern
Description
a?
Zerooroneofa
a*
Zeroormoreofa
a+
Oneormoreofa
[0-9]+
Oneormoreof0-9
a{3}
Exactly3ofa
a{3,}
3ormoreofa
a{3,6}
Between3and6ofa
a*
Greedyquantifier
a*?
Lazyquantifier
a*+
Possessivequantifier
#CommonMetacharacters
^
{
+
<
[
*
)
>
.
(
|
$
\
?
Escapethesespecialcharacterswith\
#MetaSequences
Pattern
Description
.
Anysinglecharacter
\s
Anywhitespacecharacter
\S
Anynon-whitespacecharacter
\d
Anydigit,Sameas[0-9]
\D
Anynon-digit,Sameas[^0-9]
\w
Anywordcharacter
\W
Anynon-wordcharacter
\X
AnyUnicodesequences,linebreaksincluded
\C
Matchonedataunit
\R
Unicodenewlines
\v
Verticalwhitespacecharacter
\V
Negationof\v-anythingexceptnewlinesandverticaltabs
\h
Horizontalwhitespacecharacter
\H
Negationof\h
\K
Resetmatch
\n
Matchnthsubpattern
\pX
UnicodepropertyX
\p{...}
Unicodepropertyorscriptcategory
\PX
Negationof\pX
\P{...}
Negationof\p
\Q...\E
Quote;treatasliterals
\k
Matchsubpatternname
\k'name'
Matchsubpatternname
\k{name}
Matchsubpatternname
\gn
Matchnthsubpattern
\g{n}
Matchnthsubpattern
\g
Recursenthcapturegroup
\g'n'
Recursesnthcapturegroup.
\g{-n}
Matchnthrelativeprevioussubpattern
\g
Recursenthrelativeupcomingsubpattern
\g'+n'
Matchnthrelativeupcomingsubpattern
\g'letter'
Recursenamedcapturegroupletter
\g{letter}
Matchpreviously-namedcapturegroupletter
\g
Recursesnamedcapturegroupletter
\xYY
HexcharacterYY
\x{YYYY}
HexcharacterYYYY
\ddd
Octalcharacterddd
\cY
ControlcharacterY
[\b]
Backspacecharacter
\
Makesanycharacterliteral
#Anchors
Pattern
Description
\G
Startofmatch
^
Startofstring
$
Endofstring
\A
Startofstring
\Z
Endofstring
\z
Absoluteendofstring
\b
Awordboundary
\B
Non-wordboundary
#Substitution
Pattern
Description
\0
Completematchcontents
\1
Contentsincapturegroup1
$1
Contentsincapturegroup1
${foo}
Contentsincapturegroupfoo
\x20
Hexadecimalreplacementvalues
\x{06fa}
Hexadecimalreplacementvalues
\t
Tab
\r
Carriagereturn
\n
Newline
\f
Form-feed
\U
UppercaseTransformation
\L
LowercaseTransformation
\E
TerminateanyTransformation
#GroupConstructs
Pattern
Description
(...)
Captureeverythingenclosed
(a|b)
Matcheitheraorb
(?:...)
Matcheverythingenclosed
(?>...)
Atomicgroup(non-capturing)
(?|...)
Duplicatesubpatterngroupnumber
(?#...)
Comment
(?'name'...)
NamedCapturingGroup
(?...)
NamedCapturingGroup
(?P...)
NamedCapturingGroup
(?imsxXU)
Inlinemodifiers
(?(DEFINE)...)
Pre-definepatternsbeforeusingthem
#Assertions
-
-
(?(1)yes|no)
Conditionalstatement
(?(R)yes|no)
Conditionalstatement
(?(R#)yes|no)
RecursiveConditionalstatement
(?(R&name)yes|no)
Conditionalstatement
(?(?=...)yes|no)
Lookaheadconditional
(?(?<=...)yes|no)
Lookbehindconditional
#Lookarounds
-
-
(?=...)
PositiveLookahead
(?!...)
NegativeLookahead
(?<=...)
PositiveLookbehind
(?name)
Recursesubpatternname
#POSIXCharacterClasses
CharacterClass
Sameas
Meaning
[[:alnum:]]
[0-9A-Za-z]
Lettersanddigits
[[:alpha:]]
[A-Za-z]
Letters
[[:ascii:]]
[\x00-\x7F]
ASCIIcodes0-127
[[:blank:]]
[\t]
Spaceortabonly
[[:cntrl:]]
[\x00-\x1F\x7F]
Controlcharacters
[[:digit:]]
[0-9]
Decimaldigits
[[:graph:]]
[[:alnum:][:punct:]]
Visiblecharacters(notspace)
[[:lower:]]
[a-z]
Lowercaseletters
[[:print:]]
[-~]==[[:graph:]]
Visiblecharacters
[[:punct:]]
[!"#$%&’()*+,-./:;<=>[email protected][]^_`{|}~]
Visiblepunctuationcharacters
[[:space:]]
[\t\n\v\f\r]
Whitespace
[[:upper:]]
[A-Z]
Uppercaseletters
[[:word:]]
[0-9A-Za-z_]
Wordcharacters
[[:xdigit:]]
[0-9A-Fa-f]
Hexadecimaldigits
[[:<: startofword>:]]
[\b(?<=\w)]
Endofword
#Controlverb
-
-
(*ACCEPT)
Controlverb
(*FAIL)
Controlverb
(*MARK:NAME)
Controlverb
(*COMMIT)
Controlverb
(*PRUNE)
Controlverb
(*SKIP)
Controlverb
(*THEN)
Controlverb
(*UTF)
Patternmodifier
(*UTF8)
Patternmodifier
(*UTF16)
Patternmodifier
(*UTF32)
Patternmodifier
(*UCP)
Patternmodifier
(*CR)
Linebreakmodifier
(*LF)
Linebreakmodifier
(*CRLF)
Linebreakmodifier
(*ANYCRLF)
Linebreakmodifier
(*ANY)
Linebreakmodifier
\R
Linebreakmodifier
(*BSR_ANYCRLF)
Linebreakmodifier
(*BSR_UNICODE)
Linebreakmodifier
(*LIMIT_MATCH=x)
Regexenginemodifier
(*LIMIT_RECURSION=d)
Regexenginemodifier
(*NO_AUTO_POSSESS)
Regexenginemodifier
(*NO_START_OPT)
Regexenginemodifier
#Regexexamples
#Characters
Pattern
Matches
ring
Matchringspringboardetc.
.
Matcha,9,+etc.
h.o
Matchhoo,h2o,h/oetc.
ring\?
Matchring?
\(quiet\)
Match(quiet)
c:\\windows
Matchc:\windows
Use\tosearchforthesespecialcharacters:[\^$.|?*+(){}
#Alternatives
Pattern
Matches
cat|dog
Matchcatordog
id|identity
Matchidoridentity
identity|id
Matchidoridentity
Orderlongertoshorterwhenalternativesoverlap
#Characterclasses
Pattern
Matches
[aeiou]
Matchanyvowel
[^aeiou]
MatchaNONvowel
r[iau]ng
Matchring,wrangle,sprung,etc.
gr[ae]y
Matchgrayorgrey
[a-zA-Z0-9]
Matchanyletterordigit
In[]alwaysescape.\]andsometimes^-.
#Shorthandclasses
Pattern
Meaning
\w
"Word"character(letter,digit,orunderscore)
\d
Digit
\s
Whitespace(space,tab,vtab,newline)
\W,\D,or\S
Notword,digit,orwhitespace
[\D\S]
Meansnotdigitorwhitespace,bothmatch
[^\d\s]
Disallowdigitandwhitespace
#Occurrences
Pattern
Matches
colou?r
Matchcolororcolour
[BW]ill[ieamy's]*
MatchBill,Willy,William'setc.
[a-zA-Z]+
Match1ormoreletters
\d{3}-\d{2}-\d{4}
MatchaSSN
[a-z]\w{1,7}
MatchaUWNetID
#Greedyversuslazy
Pattern
Meaning
*+{n,}greedy
Matchasmuchaspossible
<.>
Finds1bigmatchinbold
*?+?{n,}?lazy
Matchaslittleaspossible
<.>
Finds2matchesinbold
#Scope
Pattern
Meaning
\b
"Word"edge(nexttonon"word"character)
\bring
Wordstartswith"ring",exringtone
ring\b
Wordendswith"ring",exspring
\b9\b
Matchsingledigit9,not19,91,99,etc..
\b[a-zA-Z]{6}\b
Match6-letterwords
\B
Notwordedge
\Bring\B
Matchspringsandwringer
^\d*$
Entirestringmustbedigits
^[a-zA-Z]{4,20}$
Stringmusthave4-20letters
^[A-Z]
Stringmustbeginwithcapitalletter
[\.!?"')]$
Stringmustendwithterminalpuncutation
#Modifiers
Pattern
Meaning
(?i)[a-z]*(?-i)
IgnorecaseON/OFF
(?s).*(?-s)
Matchmultiplelines(causes.tomatchnewline)
(?m)^.*;$(?-m)
^&$matchlinesnotwholestring
(?x)
#free-spacingmode,thisEOLcommentignored
(?-x)
free-spacingmodeOFF
/regex/ismx
Modifymodeforentirestring
#Groups
Pattern
Meaning
(in\|out)put
Matchinputoroutput
\d{5}(-\d{4})?
USzipcode("+4"optional)
ParsertriesEACHalternativeifmatchfailsaftergroup.
Canleadtocatastrophicbacktracking.
#Backreferences
Pattern
Matches
(to)(be)ornot\1\2
Matchtobeornottobe
([^\s])\1{2}
Matchnon-space,thensametwicemore aaa,...
\b(\w+)\s+\1\b
Matchdoubledwords
#Non-capturinggroup
Pattern
Meaning
on(?:click\|load)
Fasterthan:on(click\|load)
Usenon-capturingoratomicgroupswhenpossible
#Atomicgroups
Pattern
Meaning
(?>red\|green\|blue)
Fasterthannon-capturing
(?>id\|identity)\b
Matchid,butnotidentity
"id"matches,but\bfailsafteratomicgroup,
parserdoesn'tbacktrackintogrouptoretry'identity'
Ifalternativesoverlap,orderlongertoshorter.
#Lookaround
Pattern
Meaning
(?=)
Lookahead,ifyoucanfindahead
(?!)
Lookahead,ifyoucannotfindahead
(?<=)
Lookbehind,ifyoucanfindbehind
(?>>sentence='Thisisasamplestring'
>>>bool(re.search(r'this',sentence,flags=re.I))
True
>>>bool(re.search(r'xyz',sentence))
False
#re.findall()
>>>re.findall(r'\bs?pare?\b','parsparapparentsparepartpare')
['par','spar','spare','pare']
>>>re.findall(r'\b0*[1-9]\d{2,}\b','0501035154122698234')
['0501','154','98234']
#re.finditer()
>>>m_iter=re.finditer(r'[0-9]+','453496515934204')
>>>[m[0]forminm_iterifint(m[0])<350]
['45','349','4','204']
#re.split()
>>>re.split(r'\d+','Sample123string42with777numbers')
['Sample','string','with','numbers']
#re.sub()
>>>ip_lines="catapults\nconcatenate\ncat"
>>>print(re.sub(r'^',r'*',ip_lines,flags=re.M))
*catapults
*concatenate
*cat
#re.compile()
>>>pet=re.compile(r'dog')
>>>type(pet)
>>>bool(pet.search('Theyboughtadog'))
True
>>>bool(pet.search('Acatcrossedtheirpath'))
False
#Functions
Function
Description
re.findall
Returnsalistcontainingallmatches
re.finditer
Returnaniterableofmatchobjects(oneforeachmatch)
re.search
ReturnsaMatchobjectifthereisamatchanywhereinthestring
re.split
Returnsalistwherethestringhasbeensplitateachmatch
re.sub
Replacesoneormanymatcheswithastring
re.compile
Compilearegularexpressionpatternforlateruse
re.escape
Returnstringwithallnon-alphanumericsbackslashed
#Flags
-
-
-
re.I
re.IGNORECASE
Ignorecase
re.M
re.MULTILINE
Multiline
re.L
re.LOCALE
Make\w,\b,\slocaledependent
re.S
re.DOTALL
Dotmatchesall(includingnewline)
re.U
re.UNICODE
Make\w,\b,\d,\sunicodedependent
re.X
re.VERBOSE
Readablestyle
#RegexinJavaScript
#test()
lettextA='IlikeAPPlesverymuch';
lettextB='IlikeAPPles';
letregex=/apples$/i
//Output:false
console.log(regex.test(textA));
//Output:true
console.log(regex.test(textB));
#search()
lettext='IlikeAPPlesverymuch';
letregexA=/apples/;
letregexB=/apples/i;
//Output:-1
console.log(text.search(regexA));
//Output:7
console.log(text.search(regexB));
#exec()
lettext='Doyoulikeapples?';
letregex=/apples/;
//Output:apples
console.log(regex.exec(text)[0]);
//Output:Doyoulikeapples?
console.log(regex.exec(text).input);
#match()
lettext='HereareapplesandapPleS';
letregex=/apples/gi;
//Output:["apples","apPleS"]
console.log(text.match(regex));
#split()
lettext='This593stringwillbebrok294enatplaceswhered1gitsare.';
letregex=/\d+/g
//Output:["This","stringwillbebrok","enatplaceswhered","gitsare."]
console.log(text.split(regex))
#matchAll()
letregex=/t(e)(st(\d?))/g;
lettext='test1test2';
letarray=[...text.matchAll(regex)];
//Output:["test1","e","st1","1"]
console.log(array[0]);
//Output:["test2","e","st2","2"]
console.log(array[1]);
#replace()
lettext='DoyoulikeaPPles?';
letregex=/apples/i
//Output:Doyoulikemangoes?
letresult=text.replace(regex,'mangoes');
console.log(result);
#replaceAll()
letregex=/apples/gi;
lettext='HereareapplesandapPleS';
//Output:Herearemangoesandmangoes
letresult=text.replaceAll(regex,"mangoes");
console.log(result);
#RegexinPHP
#Functions
-
-
preg_match()
Performsaregexmatch
preg_match_all()
Performaglobalregularexpressionmatch
preg_replace_callback()
Performaregularexpressionsearchandreplaceusingacallback
preg_replace()
Performaregularexpressionsearchandreplace
preg_split()
Splitsastringbyregexpattern
preg_grep()
Returnsarrayentriesthatmatchapattern
#preg_replace
$str="VisitMicrosoft!";
$regex="/microsoft/i";
//Output:VisitQuickRef!
echopreg_replace($regex,"QuickRef",$str);
#preg_match
$str="VisitQuickRef";
$regex="#quickref#i";
//Output:1
echopreg_match($regex,$str);
#preg_matchall
$regex="/[a-zA-Z]+(\d+)/";
$input_str="June24,August13,andDecember30";
if(preg_match_all($regex,$input_str,$matches_out)){
//Output:2
echocount($matches_out);
//Output:3
echocount($matches_out[0]);
//Output:Array("June24","August13","December30")
print_r($matches_out[0]);
//Output:Array("24","13","30")
print_r($matches_out[1]);
}
#preg_grep
$arr=["Jane","jane","Joan","JANE"];
$regex="/Jane/";
//Output:Jane
echopreg_grep($regex,$arr);
#preg_split
$str="Jane\tKate\nLucyMarion";
$regex="@\[email protected]";
//Output:Array("Jane","Kate","Lucy","Marion")
print_r(preg_split($regex,$str));
#RegexinJava
#Styles
#Firstway
Patternp=Pattern.compile(".s",Pattern.CASE_INSENSITIVE);
Matcherm=p.matcher("aS");
booleans1=m.matches();
System.out.println(s1);//Outputs:true
#Secondway
booleans2=Pattern.compile("[0-9]+").matcher("123").matches();
System.out.println(s2);//Outputs:true
#Thirdway
booleans3=Pattern.matches(".s","XXXX");
System.out.println(s3);//Outputs:false
#PatternFields
-
-
CANON_EQ
Canonicalequivalence
CASE_INSENSITIVE
Case-insensitivematching
COMMENTS
Permitswhitespaceandcomments
DOTALL
Dotallmode
MULTILINE
Multilinemode
UNICODE_CASE
Unicode-awarecasefolding
UNIX_LINES
Unixlinesmode
#Methods
#Pattern
Patterncompile(Stringregex[,intflags])
booleanmatches([Stringregex,]CharSequenceinput)
String[]split(Stringregex[,intlimit])
Stringquote(Strings)
#Matcher
intstart([intgroup|Stringname])
intend([intgroup|Stringname])
booleanfind([intstart])
Stringgroup([intgroup|Stringname])
Matcherreset()
#String
booleanmatches(Stringregex)
StringreplaceAll(Stringregex,Stringreplacement)
String[]split(Stringregex[,intlimit])
Therearemoremethods...
#Examples
Replacesentence:
Stringregex="[A-Z\n]{5}$";
Stringstr="IlikeAPP\nLE";
Patternp=Pattern.compile(regex,Pattern.MULTILINE);
Matcherm=p.matcher(str);
//Outputs:IlikeApple!
System.out.println(m.replaceAll("pple!"));
Arrayofallmatches:
Stringstr="ShesellsseashellsbytheSeashore";
Stringregex="\\w*se\\w*";
Patternp=Pattern.compile(regex,Pattern.CASE_INSENSITIVE);
Matcherm=p.matcher(str);
Listmatches=newArrayList<>();
while(m.find()){
matches.add(m.group());
}
//Outputs:[sells,seashells,Seashore]
System.out.println(matches);
#RegexinMySQL
#Functions
Name
Description
REGEXP
Whetherstringmatchesregex
REGEXP_INSTR()
Startingindexofsubstringmatchingregex(NOTE:OnlyMySQL8.0+)
REGEXP_LIKE()
Whetherstringmatchesregex(NOTE:OnlyMySQL8.0+)
REGEXP_REPLACE()
Replacesubstringsmatchingregex(NOTE:OnlyMySQL8.0+)
REGEXP_SUBSTR()
Returnsubstringmatchingregex(NOTE:OnlyMySQL8.0+)
#REGEXP
exprREGEXPpat
#Examples
mysql>SELECT'abc'REGEXP'^[a-d]';
1
mysql>SELECTnameFROMcitiesWHEREnameREGEXP'^A';
mysql>SELECTnameFROMcitiesWHEREnameNOTREGEXP'^A';
mysql>SELECTnameFROMcitiesWHEREnameREGEXP'A|B|R';
mysql>SELECT'a'REGEXP'A','a'REGEXPBINARY'A';
10
#REGEXP_REPLACE
REGEXP_REPLACE(expr,pat,repl[,pos[,occurrence[,match_type]]])
#Examples
mysql>SELECTREGEXP_REPLACE('abc','b','X');
aXc
mysql>SELECTREGEXP_REPLACE('abcghi','[a-z]+','X',1,2);
abcX
#REGEXP_SUBSTR
REGEXP_SUBSTR(expr,pat[,pos[,occurrence[,match_type]]])
#Examples
mysql>SELECTREGEXP_SUBSTR('abcdefghi','[a-z]+');
abc
mysql>SELECTREGEXP_SUBSTR('abcdefghi','[a-z]+',1,3);
ghi
#REGEXP_LIKE
REGEXP_LIKE(expr,pat[,match_type])
#Examples
mysql>SELECTregexp_like('aba','b+')
1
mysql>SELECTregexp_like('aba','b{2}')
0
mysql>#i:case-insensitive
mysql>SELECTregexp_like('Abba','ABBA','i');
1
mysql>#m:multi-line
mysql>SELECTregexp_like('a\nb\nc','^b$','m');
1
#REGEXP_INSTR
REGEXP_INSTR(expr,pat[,pos[,occurrence[,return_option[,match_type]]]])
#Examples
mysql>SELECTregexp_instr('aaaaaaaaa','a{3}');
2
mysql>SELECTregexp_instr('abba','b{2}',2);
2
mysql>SELECTregexp_instr('abbabba','b{2}',1,2);
5
mysql>SELECTregexp_instr('abbabba','b{2}',1,3,1);
7
Cancel