A quick reference guide for regular expressions (regex), including symbols, ranges, grouping, assertions and some sample patterns to get you started.
ShowMenu
YourFavouriteCheatSheetsYourMessagesYourBadgesYourFriendsYourCommentsViewProfileEditProfileChangePasswordLogoutNewCheatSheetNewLinkNewUploadLiveCheatSheetsDraftCheatSheetsCollaborationsLinksLogin or Register
HomeLoginRegisterCheatSheetsProgrammingCheatSheetsSoftwareCheatSheetsBusinessandMarketingCheatSheetsEducationCheatSheetsHomeandHealthCheatSheetsGamesandHobbiesCheatSheetsNewCheatSheetsPopularCheatSheetsCheatSheetsbyTagCheatSheetsbyLanguageCheatSheetLinksCreateGettingStartedCreateaNewCheatSheetUploadaCheatSheetLinktoaCheatSheetACheatSheetforMakingCheatSheets!CommunityCommunityBadgesFriendsJoinUs!LoginTwitterFacebookPinterestInstagramHelp
Cheatography
https://cheatography.com
DownloadThisCheatSheet(PDF)74CommentsRating:
()
Home
>
Programming
>
RegularExpressionsCheatSheets
RegularExpressionsCheatSheetbyDaveChildAquickreferenceguideforregularexpressions(regex),includingsymbols,ranges,grouping,assertionsandsomesamplepatternstogetyoustarted.Anchors^Startofstring,orstartoflineinmulti-linepattern\AStartofstring$Endofstring,orendoflineinmulti-linepattern\ZEndofstring\bWordboundary\BNotwordboundary\Endofword
CharacterClasses\cControlcharacter\sWhitespace\SNotwhitespace\dDigit\DNotdigit\wWord\WNotword\xHexadecimaldigit\OOctaldigit
POSIX[:upper:]Uppercaseletters[:lower:]Lowercaseletters[:alpha:]Allletters[:alnum:]Digitsandletters[:digit:]Digits[:xdigit:]Hexadecimaldigits[:punct:]Punctuation[:blank:]Spaceandtab[:space:]Blankcharacters[:cntrl:]Controlcharacters[:graph:]Printedcharacters[:print:]Printedcharactersandspaces[:word:]Digits,lettersandunderscore
Assertions?=Lookaheadassertion?!Negativelookahead?<=Lookbehindassertion?!=or?Once-onlySubexpression?()Condition[ifthen]?()|Condition[ifthenelse]?#Comment
Quantifiers*0ormore{3}Exactly3+1ormore{3,}3ormore?0or1{3,5}3,4or5Adda?toaquantifiertomakeitungreedy.
EscapeSequences\Escapefollowingcharacter\QBeginliteralsequence\EEndliteralsequence"Escaping"isawayoftreatingcharacterswhichhaveaspecialmeaninginregularexpressionsliterally,ratherthanasspecialcharacters.
CommonMetacharacters^[.${*(\+)|?<>Theescapecharacterisusually\
SpecialCharacters\nNewline\rCarriagereturn\tTab\vVerticaltab\fFormfeed\xxxOctalcharacterxxx\xhhHexcharacterhh
GroupsandRanges.Anycharacterexceptnewline(\n)(a|b)aorb(...)Group(?:...)Passive(non-capturing)group[abc]Range(aorborc)[^abc]Not(aorborc)[a-q]Lowercaseletterfromatoq[A-Q]UppercaseletterfromAtoQ[0-7]Digitfrom0to7\xGroup/subpatternnumber"x"Rangesareinclusive.
PatternModifiersgGlobalmatchi *Case-insensitivem *Multipleliness *Treatstringassinglelinex *Allowcommentsandwhitespaceinpatterne *EvaluatereplacementU *Ungreedypattern*PCREmodifier
StringReplacement$nnthnon-passivegroup$2"xyz"in/^(abc(xyz))$/$1"xyz"in/^(?:abc)(xyz)$/$`Beforematchedstring$'Aftermatchedstring$+Lastmatchedstring$&EntirematchedstringSomeregeximplementationsuse\insteadof$.
development regex regular expressions programming
DownloadtheRegularExpressionsCheatSheet
1Page
https://cheatography.com/davechild/cheat-sheets/regular-expressions/
//media.cheatography.com/storage/thumb/davechild_regular-expressions.750.jpgPDF(recommended)PDF(1page)Alternative DownloadsPDF(blackandwhite)LaTeXYourDownloadWillBeginAutomaticallyin5Seconds.Close CreatedBy
DaveChild
https://aloneonahill.comMetadataLanguages:
EnglishPublished:19thOctober,2011LastUpdated:12thMarch,2020
Rated:
5starsbasedon
359ratingsFavouritedBy
and534more...
Comments
Doug,
10:0228Nov11Yourregexcheatsheetsays^is"Startofstring"and$is"Endofstring"DaveChild,10:0228Nov11HiDoug.I'veclarifiedthatsection.Thanksfortheheadsup:)DaveChild,10:0228Nov11NoworriesKeith-easymistaketomake!,10:0228Nov11Itlooksunchangedtome.^stillsaysit's"startofstring"and$stillsays"endofstring".Theyshouldbereversed,right?,10:0228Nov11Actually,I'msorry,you'reright!Iwasprettyconfusedthere,sorryifI'veconfusedanyoneelse.
TomHunter
10:0228Nov11Canyoutagthisas'regex'?Searchingforregexoncheatographyyieldstwootherresults,butnotthisone.
Doug,
22:5212Apr12Wouldyouadd\Q...\Etothecheatsheet?
Jorge
12:164May12Itwouldbenicetoseethelistofwhitespacecharacters
Jeff
15:449May12Greatresource!Thanksforputtingthistogetherandsharing.
david
08:5820May12Whatlanguage/flavoristhis?OrIshouldalsoask,ifnonisspecified,whattendstobethedefault?I'mspecificallylookingforphporjavascript,andIknowthey'reallmostlythesame,butnot100%.
Thanks!
Chris
09:197Jun12David,Regexisprogramminglanguageneutral,asin,itdoesn'tmatterifyouareprogrammingregexexpressionsinjavascript,c#,c++,PHP,orevencommandline*nix,makesnodifference.Onlythingyouhavetowatchoutforissomeprogramminglanguagesmayrequiredifferentvariousregexcharacterstobeescapeddifferently(sotheprogramminglanguagedoesn'ttrytointerpretit).Usuallyabackslash.Forinstance\\meansONEbackslashinmanylanguages.BillSmith,20:2730Jan18Chris,Iamabitconfused.
AtthefollowingURL(https://www.regular-expressions.info/cookbook.html),Ijustreadthis:
"Thebookcoverstheregularexpressionflavors.NET,Java,JavaScript,XRegExp,Perl,PCRE,Python,andRuby,andtheprogramminglanguagesC#,Java,JavaScript,Perl,PHP,Python,Ruby,andVB.NET.Afteraquickintroduction,thebookstartswithadetailedregularexpressionstutorialwhichequallycoversall8regexflavors."
Iamhavingtroublesquaringthequotedstatementwithwhatyouwroteabove.
Thanks.
AndyGrosland
09:4528Jun12Veryhandy,thankyou!
Don'tforgetPerl;-)
littleguy
15:236Aug12Greatandusefulstuff!
Chilean
14:0323Aug12Hi,I'mtryingtolearnREGEX,andIneedtofindthis:"Page1Of60",....."Page50of60",ButIcan'tfinditusingreg.expressions!:(.Howwouldyoudothat?Thankyou!
krisw
08:5013Sep12Isthereacheatsheettothecheatsheet?Isthisinplainenglishanywhere?..."negativelookahead"..huh?
Travis
13:074Oct12@david,thischeatsheetisprettyneutral.ThemostcommonflavorisPerlCompatibleRegularExpressions(PCRE).Javascript'sengineisclosetothatandPHPalsohasPerlCompatiblefunctionsforRegex;theyusethePREGprefix.MosteverythingonthissheetshouldbesupportedbyPHP'sengine(IthinkPOSIXcharacterclassesarenot).Javascript'sengineisn'tasfeatureful.Someadvancedfeaturesaren'tsupported,butallthebasicsarethere.Ifyouneedamultilinematchandyoucan'tusetheflag,youcanuseaninvertedclassrangesuchas[\s\S]inplaceofthe.(dot)tomatchanythingincludingnewlines.
@Chilean+krisw,YouneedtofindaresourceforlearningRegularExpressions.Thischeatsheetisforreference,notlearning.Checkouthttp://www.regular-expressions.info/
fsnow55
16:1722Oct12Iwasconfusedbythefirstcomment(whichwaswrong,butyoucompoundedtheerrorwithanacknowlegement).^isthestartofstringorline.Period.
Also,yourcheatsheetisbetterorganizedthanthemorecomprehensivehttp://www.regular-expressions.info/
sinceitsmoresuccinct.Thelatterhasa1-pagesummarybutitstooverbose.
ty
12:5022Nov12Nicesheet.
Rob
11:4724Jan13Isthereareasonwhythe']'characterisnotlistedundermetacharacters?Doesn'tthatcharacterrequiretobeescapedifsearchedfor?
Bill
11:4824Jan13HeyDave.Thanksforthecheatsheet.Youmaywanttochange"Notaorborc"whenyoudescribethe[^abc}negatedcharacterclass,becauseinEnglish,thenegationisambiguous.Itcouldmean"neitheranorbenorc."Orthe"a"couldbetheonlynegateddisjunct.Youcouldmean(~av(bvc)).
Edir
09:1715Feb13Couldbeaddedtothelist.
CaseConversion
\l Makenextcharacterlowercase
\u Makenextcharacteruppercase
\L Makeentirestring(upto\E)lowercase
\U Makeentirestring(upto\E)uppercase
\u\L Capitalizefirstchar,lowercaserest(sentence)
Gabe
17:1626Mar13Ihaveadatabaseusingregex.Iamtryingtouse^filetogetallfileswithnamefile_,file,file_name_date.butitisnotworkinganyhelp
david.baird,
18:5915Jul13Is\xsupportedanywhere?Ican'tfindexamplesofitinusesearchingtheweb.ItalsodoesnotworkinascriptonmyMacintosh,OSX10.7.5usingtheOS'sperlinstallation.
DrewWhite
15:1013Feb14I'mtryingtocomeupwitharegexstringtofilterresultstoadirectorythatincludesa-zA-Zbutthatalsoincludesanunderscore('_').Doyouknowofawaytodothis?
David
15:1413Feb14Is\xsupportedanywhere?Ican'tseemtofindwhereitissupported.E.g.,perlonMacOS10.7.5.Searchingforon-lineexamplesorhelpalsofails,inthatnooneknowsaboutit.
David
15:1413Feb14Is\x(RegularExpressionsCharacterClasses)supportedanywhere?Isthisanewclassthathasjustbeenadded,becauseIamunabletouseitinworkingwithIPV6addresses.Searchingforastringcontainingsomethinglike2001::1a79withaRegEx2001::\x{1,4}willfail,butifIuse2001::[a-fA-F0-9]{1,4}willwork.
David
15:1413Feb14\xisatermin"RegularExpressionsCharacterClasses"foranhexadecimaldigit.Howdoesthiscomparetothe\xhh"SpecialCharacters"?Isitsupportedtoday?
Benoit
08:5414Feb14Itwouldbegreattoincreaseinsomewaysthe-->:Unixlines=>Java
(?i)=>Caseinsensitive=>PCRE,Perl,Java
(?J)=>Allowduplicatenames=>PCRE*
(?m)=>Multiline=>PCRE,Perl,Java
(?s)=>Singleline(dotall)=>PCRE,Perl,Java
(?u)=>Unicodecase=>Java
(?U)=>Defaultmatchlazy=>PCRE
(?x)=>Ignorewhitespace,comments=>PCRE,Perl,Java
(?-…)=>Unsetorturnoffoptions=>PCRE
aliaksandr,
04:4927Jan21Syntax=>Description
(?…)=>Anamedgroup
(?name…)=>Anothernamedgroup
(?P…)=>AnamedgroupinPython
\k=>ReferencebynameinPerl
\k'name'=>ReferencebynameinPerl
\g{name}=>ReferencebynameinPerl
\k{name}=>Referencebynamein.NET
(?P=name)=>ReferencebynameinPython
aliaksandr,
22:4929Jan21Lazyquantifiers
Syntax=>Description
??=>Lazyzeroorone(optional)
+?=>Lazyoneormore
*?=>Lazyzeroormore
{n}?=>Lazyn
{n,}?=>Lazynormore
{m,n}?=>Lazym,nAddaComment
YourCommentPleaseenteryourname.YourNamePleaseenteryouremailaddressYourEmailAddressPleaseenteryourComment.YourCommentPostYourCommentRelatedCheatSheetsPerlReferenceCardCheatSheetThisisversion2oftheperlreferencecard.mishin2Jun126Pagesdevelopment,regex,programming,perl .NETRegularExpressionsCheatSheetbyRegExLib.comAresourceforregularexpressionsin.NET.Cheatography3Jun14development,regex,regular,expressions,programmingand2more ...pythonregularexpression(regex)CheatSheetpythonregularexpressionsmutanclan19Apr193Pagesdevelopment,python,regex,regular,programming,expressionMoreCheatSheetsbyDaveChildLinuxCommandLineCheatSheetAcheatsheetofthecommandsIusemostforLinux,withpopuplinkstomanpages.DaveChild28Oct112Pagesbash,nano,linux,ls,commandlineand4more ...CSS2CheatSheetAquickreferenceguideforCSS,listingselectorsyntax,properties,unitsandotherusefulbitsofinformation.DaveChild19Oct112Pagescss,webdesign,web,design,frontendmod_rewriteCheatSheetAmod_rewriteCheatSheet-aquickreferenceguideformod_rewrite,withrewriteflags,regularexpressionsyntaxandsamplerules.DaveChild19Oct112Pagesdevelopment,rewrite,mod-rewrite,apache,lampand4more ...LatestCheatSheet2Pages (0)Rest-Assured:ExtractvaluebyGPathCheatSheetThisisforRest-Assuredcourseinhttps://giangtester.com/Giang.nd25087Jul22RandomCheatSheet2Pages (2)GIII:StomachCheatSheetGastroenterologyksellybelly6Aug14,updated12May16medical,disease,gastroenterology,stomach,neoplasm,pudAboutCheatographyCheatographyisacollectionof5167cheatsheetsandquickreferencesin25languagesforeverythingfromtraveltohistory!BehindtheScenesIfyouhaveanyproblems,orjustwanttosayhi,youcanfindusrighthere:DaveChildSpaceDuckCheatographyRecentActivityGiang.nd2508publishedRest-Assured:ExtractvaluebyGPath.2hours9minsagorehman225publishedPhysics-Elementsofvectors.11hours17minsagoomarffkamelpublishedPandas.17hours5minsagoBrandonGenepublishedPop!_OS.17hours16minsagocaptain_deerpublishedGermanA1.19hours25minsago
©2011-2022Cheatography.com | CCLicense | Terms | Privacy
LatestCheatSheetsRSSFeed