module RE
[Character Group]
Indicates that the various characters that may appear in the same position form a group of characters, expressed in a regular expression using []
Characters are divided into classes, such as numbers, letters, punctuation, etc.
A string that represents a number:
Abbreviated mode must be from small to large
Group of characters representing letters
[ABCD]
[A-d]
means match any character [\w\w][\s\s][\d\d]
The common methods under the RE module are:
ImportReret= Re.findall ('a','Eva Egon Yuan')#returns all results that meet the matching criteria, placed in the listPrint(ret)#result: [' A ', ' a ']ret= Re.search ('a','Eva Egon Yuan'). Group ()Print(ret)#result: ' A '#The function finds a pattern match within the string, only to find the first match and then returns an object that contains the matching information that can be#a matching string is obtained by calling the group () method, and none is returned if the string does not match. ret= Re.match ('a','ABC'). Group ()#same as search, but match at the beginning of the stringPrint(ret)#result: ' A 'ret= Re.split ('[AB]','ABCD')#first Press ' a ' to split the ' and ' BCD ', in the pair ' and ' BCD ' separate by ' B 'Print(ret)#[', ', ', ' CD ']ret= Re.sub ('\d','H','Eva3egon4yuan4', 1)#replace number with ' H ', parameter 1 means replace only 1Print(ret)#Evahegon4yuan4ret= Re.subn ('\d','H','Eva3egon4yuan4')#replace the number with ' H ', return the tuple (replace the result, replace the number of times)Print(ret) obj= Re.compile ('\d{3}')#compiles a regular expression into a regular expression object, with the rule matching 3 digitsret = Obj.search ('abc123eeee')#The regular Expression object calls search, and the argument is the string to be matchedPrint(Ret.group ())#results: 123ImportReret= Re.finditer ('\d','ds3sy4784a')#Finditer Returns an iterator that holds the matching resultPrint(ret)#<callable_iterator object at 0x10195f940>Print(Next (Ret). Group ())#View the first resultPrint(Next (Ret). Group ())#View the second resultPrint([I.group () forIinchRET])#view remaining left and right results
Python-re Module