The study of regular expressions
#Codeing:utf-8#__author__:duke#date:2018/3/17/017 23:29ImportRe#method One findall full searchret = Re.findall ('w\w{2}','Hello World')Print(ret)#Meta-Characters of 11# . Wildcard charactersret = Re.findall ('W..','Hello World')#w: Dot any finger, one generation refers to one characterPrint(ret) RET= Re.findall ('W.L','Hello W\rld')#w: Dot any finger, one generation refers to one characterPrint(ret)#cannot refer to "\" character#^ Sharp angle characterret = Re.findall ("^H...O","Hjasdfhello")#match only from startPrint(ret)#$ dollor Symbolret = Re.findall ("d.. e$","Hjasdukefhflle")#match only from the endPrint(ret)#* Repeat match [0 Infinity]ret = Re.findall ("du*","hjasdukefhfllfdukeeerffrefwegduuuuekw")#match only from the endPrint(ret)#+ repeat match [1 Infinity]ret = Re.findall ("du+","hjasdukefhfllfdkeeerffrefwegduuuuekw")Print(ret)#at least one U## + Repeat match [1 infinity] eachret = Re.findall ("du+","hjasdukefhfllfdkeeerffrefwegduuuuekw")Print(ret)#at least one U#? 0 or 1 Dret = Re.findall ("D?u","hjasdukefhfllfdkeeerffrefwegduuuuekw")Print(ret)#{} can be used within a rangeret = Re.findall ("du{1,2}","hjasdukefhfllfdkeeerffrefwegduuuuekw")Print(ret)#greedy match, by the most match#Conclusion: * =={0,+ Infinity} + = = {1,+ Infinity}? = = {0,1}#[] Character setret = Re.findall ('a[c,d,e,s,]x','ASX')#Yes or meaningPrint(ret) RET= Re.findall ('[A-z]','ASX')#-Indication RangePrint(ret)#note the [] Character set: Cancels the function of the metacharacters, (\ ^-) except for the threeret = Re.findall ('[w,,]','awdx*,')#* changed to normal characterPrint(ret) RET= Re.findall ('[^a]','awdx*,')#^ The complement set is the element except aPrint(ret) RET= Re.findall ('[^a,w]','awdx*,')#all elements in [] are not included,Print(ret)#[' d ', ' X ', ' * ']#\ Slash#meta character removal of special functions after slash#some ordinary characters after the slash implement special functions#\d = number [0,9]Print(Re.findall ('\d{11}','hqfuhi8712648721485'))#Match 11 digits#[' 87126487214 ']#\d said [^0=9]Print(Re.findall ('\d{5}','hqfuhi8712648721485'))#Match 5 characters## [' Hqfuh ']## \s denotes any null character]Print(Re.findall ('\s','hqfu\thi87 1264\r8721485'))## [' \ t ', ', ' \ R ']## \s denotes any non-null character]Print(Re.findall ('\s','hqfu\thi87 1264\r8721485'))#[' h ', ' Q ', ' f ', ' u ', ' h ', ' I ', ' 8 ', ' 7 ', ' 1 ', ' 2 ', ' 6 ', ' 4 ', ' 8 ', ' 7 ', ' 2 ',#\w denotes any numeric alphabetic character]#\w denotes any non-numeric alphabetic character]#\b Represents the bounds of a special characterPrint(Re.findall (R'\bi','hello i an Duke')) #\+ret = Re.search ('A +','aaawdx*,')#Print(Ret.group ())#AAAret = Re.search ('a\+','aaawdx*,')#Print(ret)#None#two ways to solve the red flagret = Re.findall (r'\\','aaawdxd:\c')#Print(ret)#[' \ \ ']ret = Re.findall ('\\\\','aaawdxd:\c')#Print(ret)#[' \ \ ']#use the results to understandret = Re.search (r'\bduke','Duke')#r = Native StringPrint(Ret.group ())#Dukeret = Re.search ('\bduke','Duke')#r = Native StringPrint(ret)#None#() do group |Print(Re.search ('(AS) +','sdjkasf'). Group ())# asPrint(Re.search ('(as+)','sdjkasf'). Group ())# asPrint(Re.search ('(AS) |3','sdj3kas3f3'). Group ())#whole orPrint(Re.findall ('(AS) |3','sdj3kas3f3') )#[', ' as ', ', ', ']##用于文件路径ret = Re.search ('(? P<ID>\D{3})/(? P<NAME>\W{3})','dghwwweeejyaf123/ooo')Print(Ret.group ())Print(Ret.group ('ID'))Print(Ret.group ('name'))#Second: The method of re#findall (): Returns the full result to the list of the situation#re.search (): Returns an object that returns the result through the group method#Re.match () only starts the match at the beginning of the string and returns an object group#re.split ():Print(Re.split ('[K,s]','Dukehsijik'))#this is important .Print(Re.split ('[K,s]','Dskehsijik'))#pay attention to the Division method#Sub () substitutionPrint(Re.sub ('d.. E','haha','JIEWJFREWDUKESHJS')) Principle= Re.compile ('d.. E')#Compiling rulesret = Principle.findall ('IEWJFREWDUKESHJS')Print(ret)#[' Duke ']
Python Learning Day12