Python - Regular Expressions - Tutorialspoint

文章推薦指數: 80 %
投票人數:10人

Python - Regular Expressions ... A regular expression is a special sequence of characters that helps you match or find other strings or sets of strings, using a ... Home CodingGround Jobs Whiteboard Tools Business Teachwithus PythonBasicTutorial Python-Home Python-Overview Python-EnvironmentSetup Python-BasicSyntax Python-VariableTypes Python-BasicOperators Python-DecisionMaking Python-Loops Python-Numbers Python-Strings Python-Lists Python-Tuples Python-Dictionary Python-Date&Time Python-Functions Python-Modules Python-FilesI/O Python-Exceptions PythonAdvancedTutorial Python-Classes/Objects Python-RegExpressions Python-CGIProgramming Python-DatabaseAccess Python-Networking Python-SendingEmail Python-Multithreading Python-XMLProcessing Python-GUIProgramming Python-FurtherExtensions PythonUsefulResources Python-QuestionsandAnswers Python-QuickGuide Python-Tools/Utilities Python-UsefulResources Python-Discussion SelectedReading UPSCIASExamsNotes Developer'sBestPractices QuestionsandAnswers EffectiveResumeWriting HRInterviewQuestions ComputerGlossary WhoisWho Python-RegularExpressions Advertisements PreviousPage NextPage  Aregularexpressionisaspecialsequenceofcharactersthathelpsyoumatchorfindotherstringsorsetsofstrings,usingaspecializedsyntaxheldinapattern.RegularexpressionsarewidelyusedinUNIXworld. ThePythonmodulereprovidesfullsupportforPerl-likeregularexpressionsinPython.Theremoduleraisestheexceptionre.errorifanerroroccurswhilecompilingorusingaregularexpression. Wewouldcovertwoimportantfunctions,whichwouldbeusedtohandleregularexpressions.Butasmallthingfirst:Therearevariouscharacters,whichwouldhavespecialmeaningwhentheyareusedinregularexpression.Toavoidanyconfusionwhiledealingwithregularexpressions,wewoulduseRawStringsasr'expression'. ThematchFunction ThisfunctionattemptstomatchREpatterntostringwithoptionalflags. Hereisthesyntaxforthisfunction− re.match(pattern,string,flags=0) Hereisthedescriptionoftheparameters− Sr.No. Parameter&Description 1 pattern Thisistheregularexpressiontobematched. 2 string Thisisthestring,whichwouldbesearchedtomatchthepatternatthebeginningofstring. 3 flags YoucanspecifydifferentflagsusingbitwiseOR(|).Thesearemodifiers,whicharelistedinthetablebelow. There.matchfunctionreturnsamatchobjectonsuccess,Noneonfailure.Weusegroup(num)orgroups()functionofmatchobjecttogetmatchedexpression. Sr.No. MatchObjectMethod&Description 1 group(num=0) Thismethodreturnsentirematch(orspecificsubgroupnum) 2 groups() Thismethodreturnsallmatchingsubgroupsinatuple(emptyifthereweren'tany) Example LiveDemo #!/usr/bin/python importre line="Catsaresmarterthandogs" matchObj=re.match(r'(.*)are(.*?).*',line,re.M|re.I) ifmatchObj: print"matchObj.group():",matchObj.group() print"matchObj.group(1):",matchObj.group(1) print"matchObj.group(2):",matchObj.group(2) else: print"Nomatch!!" Whentheabovecodeisexecuted,itproducesfollowingresult− matchObj.group():Catsaresmarterthandogs matchObj.group(1):Cats matchObj.group(2):smarter ThesearchFunction ThisfunctionsearchesforfirstoccurrenceofREpatternwithinstringwithoptionalflags. Hereisthesyntaxforthisfunction− re.search(pattern,string,flags=0) Hereisthedescriptionoftheparameters− Sr.No. Parameter&Description 1 pattern Thisistheregularexpressiontobematched. 2 string Thisisthestring,whichwouldbesearchedtomatchthepatternanywhereinthestring. 3 flags YoucanspecifydifferentflagsusingbitwiseOR(|).Thesearemodifiers,whicharelistedinthetablebelow. There.searchfunctionreturnsamatchobjectonsuccess,noneonfailure.Weusegroup(num)orgroups()functionofmatchobjecttogetmatchedexpression. Sr.No. MatchObjectMethods&Description 1 group(num=0) Thismethodreturnsentirematch(orspecificsubgroupnum) 2 groups() Thismethodreturnsallmatchingsubgroupsinatuple(emptyifthereweren'tany) Example LiveDemo #!/usr/bin/python importre line="Catsaresmarterthandogs"; searchObj=re.search(r'(.*)are(.*?).*',line,re.M|re.I) ifsearchObj: print"searchObj.group():",searchObj.group() print"searchObj.group(1):",searchObj.group(1) print"searchObj.group(2):",searchObj.group(2) else: print"Nothingfound!!" Whentheabovecodeisexecuted,itproducesfollowingresult− searchObj.group():Catsaresmarterthandogs searchObj.group(1):Cats searchObj.group(2):smarter MatchingVersusSearching Pythonofferstwodifferentprimitiveoperationsbasedonregularexpressions:matchchecksforamatchonlyatthebeginningofthestring,whilesearchchecksforamatchanywhereinthestring(thisiswhatPerldoesbydefault). Example LiveDemo #!/usr/bin/python importre line="Catsaresmarterthandogs"; matchObj=re.match(r'dogs',line,re.M|re.I) ifmatchObj: print"match-->matchObj.group():",matchObj.group() else: print"Nomatch!!" searchObj=re.search(r'dogs',line,re.M|re.I) ifsearchObj: print"search-->searchObj.group():",searchObj.group() else: print"Nothingfound!!" Whentheabovecodeisexecuted,itproducesthefollowingresult− Nomatch!! search-->searchObj.group():dogs SearchandReplace Oneofthemostimportantremethodsthatuseregularexpressionsissub. Syntax re.sub(pattern,repl,string,max=0) ThismethodreplacesalloccurrencesoftheREpatterninstringwithrepl,substitutingalloccurrencesunlessmaxprovided.Thismethodreturnsmodifiedstring. Example LiveDemo #!/usr/bin/python importre phone="2004-959-559#ThisisPhoneNumber" #DeletePython-stylecomments num=re.sub(r'#.*$',"",phone) print"PhoneNum:",num #Removeanythingotherthandigits num=re.sub(r'\D',"",phone) print"PhoneNum:",num Whentheabovecodeisexecuted,itproducesthefollowingresult− PhoneNum:2004-959-559 PhoneNum:2004959559 RegularExpressionModifiers:OptionFlags Regularexpressionliteralsmayincludeanoptionalmodifiertocontrolvariousaspectsofmatching.Themodifiersarespecifiedasanoptionalflag.YoucanprovidemultiplemodifiersusingexclusiveOR(|),asshownpreviouslyandmayberepresentedbyoneofthese− Sr.No. Modifier&Description 1 re.I Performscase-insensitivematching. 2 re.L Interpretswordsaccordingtothecurrentlocale.Thisinterpretationaffectsthealphabeticgroup(\wand\W),aswellaswordboundarybehavior(\band\B). 3 re.M Makes$matchtheendofaline(notjusttheendofthestring)andmakes^matchthestartofanyline(notjustthestartofthestring). 4 re.S Makesaperiod(dot)matchanycharacter,includinganewline. 5 re.U InterpretslettersaccordingtotheUnicodecharacterset.Thisflagaffectsthebehaviorof\w,\W,\b,\B. 6 re.X Permits"cuter"regularexpressionsyntax.Itignoreswhitespace(exceptinsideaset[]orwhenescapedbyabackslash)andtreatsunescaped#asacommentmarker. RegularExpressionPatterns Exceptforcontrolcharacters,(+?.*^$()[]{}|\),allcharactersmatchthemselves.Youcanescapeacontrolcharacterbyprecedingitwithabackslash. FollowingtableliststheregularexpressionsyntaxthatisavailableinPython− Sr.No. Pattern&Description 1 ^ Matchesbeginningofline. 2 $ Matchesendofline. 3 . Matchesanysinglecharacterexceptnewline.Usingmoptionallowsittomatchnewlineaswell. 4 [...] Matchesanysinglecharacterinbrackets. 5 [^...] Matchesanysinglecharacternotinbrackets 6 re* Matches0ormoreoccurrencesofprecedingexpression. 7 re+ Matches1ormoreoccurrenceofprecedingexpression. 8 re? Matches0or1occurrenceofprecedingexpression. 9 re{n} Matchesexactlynnumberofoccurrencesofprecedingexpression. 10 re{n,} Matchesnormoreoccurrencesofprecedingexpression. 11 re{n,m} Matchesatleastnandatmostmoccurrencesofprecedingexpression. 12 a|b Matcheseitheraorb. 13 (re) Groupsregularexpressionsandremembersmatchedtext. 14 (?imx) Temporarilytogglesoni,m,orxoptionswithinaregularexpression.Ifinparentheses,onlythatareaisaffected. 15 (?-imx) Temporarilytogglesoffi,m,orxoptionswithinaregularexpression.Ifinparentheses,onlythatareaisaffected. 16 (?:re) Groupsregularexpressionswithoutrememberingmatchedtext. 17 (?imx:re) Temporarilytogglesoni,m,orxoptionswithinparentheses. 18 (?-imx:re) Temporarilytogglesoffi,m,orxoptionswithinparentheses. 19 (?#...) Comment. 20 (?=re) Specifiespositionusingapattern.Doesn'thavearange. 21 (?!re) Specifiespositionusingpatternnegation.Doesn'thavearange. 22 (?>re) Matchesindependentpatternwithoutbacktracking. 23 \w Matcheswordcharacters. 24 \W Matchesnonwordcharacters. 25 \s Matcheswhitespace.Equivalentto[\t\n\r\f]. 26 \S Matchesnonwhitespace. 27 \d Matchesdigits.Equivalentto[0-9]. 28 \D Matchesnondigits. 29 \A Matchesbeginningofstring. 30 \Z Matchesendofstring.Ifanewlineexists,itmatchesjustbeforenewline. 31 \z Matchesendofstring. 32 \G Matchespointwherelastmatchfinished. 33 \b Matcheswordboundarieswhenoutsidebrackets.Matchesbackspace(0x08)wheninsidebrackets. 34 \B Matchesnonwordboundaries. 35 \n,\t,etc. Matchesnewlines,carriagereturns,tabs,etc. 36 \1...\9 Matchesnthgroupedsubexpression. 37 \10 Matchesnthgroupedsubexpressionifitmatchedalready.Otherwisereferstotheoctalrepresentationofacharactercode. RegularExpressionExamples Literalcharacters Sr.No. Example&Description 1 python Match"python". Characterclasses Sr.No. Example&Description 1 [Pp]ython Match"Python"or"python" 2 rub[ye] Match"ruby"or"rube" 3 [aeiou] Matchanyonelowercasevowel 4 [0-9] Matchanydigit;sameas[0123456789] 5 [a-z] MatchanylowercaseASCIIletter 6 [A-Z] MatchanyuppercaseASCIIletter 7 [a-zA-Z0-9] Matchanyoftheabove 8 [^aeiou] Matchanythingotherthanalowercasevowel 9 [^0-9] Matchanythingotherthanadigit SpecialCharacterClasses Sr.No. Example&Description 1 . Matchanycharacterexceptnewline 2 \d Matchadigit:[0-9] 3 \D Matchanondigit:[^0-9] 4 \s Matchawhitespacecharacter:[\t\r\n\f] 5 \S Matchnonwhitespace:[^\t\r\n\f] 6 \w Matchasinglewordcharacter:[A-Za-z0-9_] 7 \W Matchanonwordcharacter:[^A-Za-z0-9_] RepetitionCases Sr.No. Example&Description 1 ruby? Match"rub"or"ruby":theyisoptional 2 ruby* Match"rub"plus0ormoreys 3 ruby+ Match"rub"plus1ormoreys 4 \d{3} Matchexactly3digits 5 \d{3,} Match3ormoredigits 6 \d{3,5} Match3,4,or5digits Nongreedyrepetition Thismatchesthesmallestnumberofrepetitions− Sr.No. Example&Description 1 <.> Greedyrepetition:matches"perl>" 2 <.> Nongreedy:matches""in"perl>" GroupingwithParentheses Sr.No. Example&Description 1 \D\d+ Nogroup:+repeats\d 2 (\D\d)+ Grouped:+repeats\D\dpair 3 ([Pp]ython(,)?)+ Match"Python","Python,python,python",etc. Backreferences Thismatchesapreviouslymatchedgroupagain− Sr.No. Example&Description 1 ([Pp])ython&\1ails Matchpython&pailsorPython&Pails 2 (['"])[^\1]*\1 Singleordouble-quotedstring.\1matcheswhateverthe1stgroupmatched.\2matcheswhateverthe2ndgroupmatched,etc. Alternatives Sr.No. Example&Description 1 python|perl Match"python"or"perl" 2 rub(y|le)) Match"ruby"or"ruble" 3 Python(!+|\?) "Python"followedbyoneormore!orone? Anchors Thisneedstospecifymatchposition. Sr.No. Example&Description 1 ^Python Match"Python"atthestartofastringorinternalline 2 Python$ Match"Python"attheendofastringorline 3 \APython Match"Python"atthestartofastring 4 Python\Z Match"Python"attheendofastring 5 \bPython\b Match"Python"atawordboundary 6 \brub\B \Bisnonwordboundary:match"rub"in"rube"and"ruby"butnotalone 7 Python(?=!) Match"Python",iffollowedbyanexclamationpoint. 8 Python(?!!) Match"Python",ifnotfollowedbyanexclamationpoint. SpecialSyntaxwithParentheses Sr.No. Example&Description 1 R(?#comment) Matches"R".Alltherestisacomment 2 R(?i)uby Case-insensitivewhilematching"uby" 3 R(?i:uby) Sameasabove 4 rub(?:y|le)) Grouponlywithoutcreating\1backreference PreviousPage PrintPage NextPage  Advertisements



請為這篇文章評分?