Import Re # findAll # Search # match # RET = the re.findall ( 'A', 'EVA Egon Yuan') # # returns all results satisfy a condition on the list # Print (RET) # RET = the re.search ( 'a', 'EVA Egon Yuan') # IF RET: # Print (ret.group ()) # # front to back, find a return, return variables need to call the group to get results # # If none is found, the return None, being given group call # RET = re.match ( '[AZ] +', 'EVA Egon Yuan') # IF RET: # Print (ret.group ()) # # match scratch match, if regular starting from scratch can be a match, it returns a variable # content # match to be displayed with a group ## If no matches, return None, being given group call # RET = re.split ( '[ab &]', 'ABCD') # Print (RET) # [ '', 'CD'] # # press' a 'split by' and 'bcd'. respectively 'b' is divided # RET = the re.sub ( '\ D', 'H', 'eva3egon4yuan4',. 1) # Print (RET) # # the number substitution to 'H', the parameter 1 replaces only a # RET = re.subn ( '\ D', 'H', 'eva3egon4yuan4') # Print (RET) # # digital replaced 'H', returns a tuple (Alternatively a result, the number of times the replacement) # the compile a regular expression compiler become the object # obj = the re.compile ( '\ D {}. 3') # # regular expression compiled into a regular expression object, the rule to be matched is three digits # RET = obj.search ( 'abc123eee') # Object calls the regular expression search, the parameters to be matched string # Print (ret.group ()) # Results: 123 #finditer iteration # RET = re.finditer ( '\ D', 'ds3su4784a') # finditer returns a match result iterator # # Print (RET) # <Object callable_iterator AT 0x000001BC77390E48> # # Print (Next (RET) .group ()) to view the first result # # # Print (Next (RET) .group ()) to view the second result # # # Print ([i.group () in RET for I]) to view the remaining results # # I in RET for: # Print (i.group ()) # packet # RET = the re.search ( '^ [1-9] (\ D {14}) (\ D {2} [0-9x])' , '411403198606166078') # Print (ret.group ()) # Print (ret.group (. 1)) # Print (ret.group (2)) # regular expression, there are several packets can have several variables
note:
1 findall priority query:
# RET = re.findall ( 'the WWW (baidu |. Oldboy) .com', 'www.oldboy.com') # Print (RET) # [ 'Oldboy'] # This is because findall will give priority to the group matches content back, if you want to match results, canceled privileges to # RET = re.findall ( 'the WWW (?: baidu |. Oldboy) .com', 'www.oldboy.com') # Print (RET) # [ ' www.oldboy.com ']
2 split of priority query
= re.split RET ( " \ D + " , " eva3egon4yuan " ) Print (RET) # Results: [ 'EVA', 'Egon', 'Yuan'] RET = re.split ( " (\ + D) " , " eva3egon4yuan " ) Print (RET) # results: [ 'EVA', '. 3', 'Egon', '. 4', 'Yuan'] # after the matching section plus () cut out of the results are different, # no (a) does not match the item retained, but there are () but can retain the matches, # the need to keep the course in some part of the match is very important.
Exercises and expansion