Regex Cheat Sheet/Basics - - Rackspace
文章推薦指數: 80 %
Regular Expressions (regex or regexp) are a very useful tool to identify specific patterns in any text, which helps to extract information ... End-to-EndMulticloudSolutions. SolvingTogether.™ LearnmoreatRackspace.com How–ToHome CloudServers Introduction FAQ AllArticles Editthisarticle RegexCheatSheet/Basics Lastupdatedon: 2021-08-13 Authoredby: AlfonsoMurillo RegexCheatSheet/Basics RegularExpressions(regexorregexp)areaveryusefultooltoidentifyspecificpatternsinanytext,whichhelpstoextractinformationregardlesstheformatofthetext. Regexcanbeusedtovalidateinputs,webscrapping,findingspecificstringsindocuments,syntaxvalidationforcompilers,andsomanyothersexamples. Regexiswidelyusedinmultipleprogramminglanguagesusingalmostthesamesyntax,sothisarticlepretendstoshowthebasicregexoperators. Thefollowingsectionswillshowthedifferentoperatorsusedforregexandsomeexamples.Youcanalsotestyourregularexpressionswithsomeexplanationsofthemonthispage. Anchors Characters Use ^ Indicatesthebeginningofthestring.Itwillonlymatchthestringthatbeginswiththecharacter(s)thatfollows $ Indicatestheendofthestring.Thiswillmatchanystringthatendswiththecharacter(s)beforethe‘$’symbol abc Matchesanystringthatcontainsthe‘abc’characterssequenceonit Someexamplesare: Example Description Matchingstringexamples ^A MatchesallthestringsthatstartwithanA “Anappleisinthetree”,“Anewrestaurant” end$ Matchesastringthatendswiththe‘end’sequenceofcharacters “Theend”,“Let’spretend” ^Helloworld$ Matchestheexactstring‘Helloworld’ “Helloworld” order Matchesanystringthatcontainsthetext‘order’init “Myordernumberis54” Quantifiers Quantifiersareusedtorepresentthetimeswewantthepreecedingcharacterorgroupofcharacterstoappearinourmatch. Characters Use * Indicateszeroormore + Indicatesoneoremore ? Indicateszeroorone x{n} Usedtospecifythenumberoftimes(‘n’)thepreviouscharacter(‘x’)shouldappear x{n,} Usedtospecifytheminimumnumberoftimes(‘n’)thepreviouscharacter(‘x’)shouldappear x{n,m} Usedtospecifytheminimum(‘n’)andmaximum(’m')numberoftimesthepreviouscharacter(‘x’)shouldappear Someexamplesare: Example Description Matchingstringexamples hello!* Matchesanystring‘hello’followedbyzeroormore‘!’characters “hello”,“hello!”,hello!! hello!+ Matchesanystring‘hello’followedbyoneormore‘!’characters “hello!”,“hello!!” hello!? Matchesanystring‘hello’followedbyzeroorone‘!’characters “hello”,“hello!” (ha){2,4} Matchesanystringthatrepeatsthegroupofcharacters‘ha’twouptofourtimes “haha”,“hahaha”,“hahahaha” ORoperator Characters Use a|b Matchesanystringthatcontainseither‘a’or‘b’ [] Isusedtorepresentalist,soitmatchesastringthatcontainsoneofthecharactersinsidethelist Someexamplesare: Example Description Matchingstringexamples se(a|e) Matchesanystringthatcontainsthetext‘se’followedeitherbyan‘a’oran‘e’ “see”,“sea” a[bcd] Matchesanystringthatcontainsan‘a’followedeitherby‘b’,‘c’,or’d' “ab”,“ac”,“ad” Characterclasses Thecharacterclassesoperatorsallowyoutomatchcharactersinsideacategory(class).Theseoperatorsoffertheirnegations,whicharethesameasthenormaloperatorbutinuppercase. Characters Use \d Matchesasingledigitcharacter \w Matchesasinglewordcharacter(letters,numbers,andunderscore) \s Matchesasinglewhitespacecharacter,includingtabsandlinebreaks \D Matchesasinglenon-digitcharacter \W Matchesasinglenon-wordcharacter(letters,numbers,andunderscore) \S Matchesasinglenon-whitespacecharacter,includingtabsandlinebreaks . Matchesanysinglecharacter Flags Regexusuallyusestheform/pattern/.Attheend,wecanusethefollowingflags: Flag Description g Theglobalflagisusedtosearchforalltheindividualmatchesinsidethestring.Ifitisnotused,theexpressionwillreturnafterthefirstmatch m Themultilineflagallowstouse^and$asthebeginningandendofaline,notthebeginningandendofthestring,whichcancontainmultiplelines i Theinsensitiveflagmakestheregularexpressioncaseinsensitive Conclusions Regexhasalotofuses.Youcancombinethesimpleoperatorsexplainedinthisarticletocreatecomplexpatternsearches.Oneexampleistovalidateanemailaddress,thiscanbedondewiththefollowingregularexpression: ^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$ Thisexamplematchesacompletestringforwhichitsearchesapatternwiththefollowingorder: [\w-\.]+:acombinationofoneormorewordcharacters,hyphensordots @:the@symbol ([\w-]+\.)+:oneormorecombinationsofoneormorewordcharactersorhyphensendedbyadot(fordomainsandsubdomainsontheemaillikeemail.rackspace.) [\w-]{2,4}:acombinationfor2,3or4wordcharactersorhyphensfortheendpartoftheemail(com) Justlikethisexampletherearemanyothersthatcanbeeasilyimplementedfordifferentpurposes. Sharethisinformation: ©2020RackspaceUS,Inc. Exceptwhereotherwisenoted,contentonthissiteislicensedunderaCreativeCommonsAttribution-NonCommercial-NoDerivs3.0UnportedLicense SeelicensespecificsandDISCLAIMER
延伸文章資訊
- 1Regex Cheat Sheet/Basics - - Rackspace
Regular Expressions (regex or regexp) are a very useful tool to identify specific patterns in any...
- 2regexp cheatsheet - Devhints
Basic cheatsheets for regular expression · One-page guide to regexp.
- 3RegEX Cheat Sheet & Quick Reference
This is a quick cheat sheet to getting started with regular expressions. ... re.compile, Compile ...
- 4RegExLib.com Regular Expression Cheat Sheet (.NET)
RegExLib.com Regular Expression Cheat Sheet (.NET). Metacharacters Defined. MChar, Definition. ^,...
- 5Regular Expressions cheat sheet
Regular Expressions cheat sheet. Basic matching. Each symbol matches a single character: anything...