python regular expressions re module common method

There are major findall, search, match, split, sub, subn, compile, finditer method

 

findall method

= the re.findall RET ( ' A ' , ' EVA Egon Yuan ' )   # return all matching result satisfies the condition, in the list 
Print (RET) # Results: [ 'a', 'a ']

 

search method

= the re.search RET ( ' A ' , ' EVA Egon Yuan ' ) .group ()
 Print (RET) # Results: 'A' 
# function looks for the string pattern matching, only the first to find a match then returns matching information comprising an object, the object may 
# method of string matching obtained by calling Group (), if no matching string, None is returned

 

match method

= re.match RET ( ' A ' , ' ABC ' ) .group ()   # same search, from the beginning of the string matching most 
Print (RET)
 # Results: 'a'

 

split method

= re.split RET ( ' [ab &] ' , ' ABCD ' )   # press the 'a' obtained by dividing 'and' bcd ', of the' and 'BCD' by 'b' are divided 
Print (RET)   # [ '', '', ' cd']

 

sub Method

= the re.sub RET ( ' \ D ' , ' H ' , ' eva3egon4yuan4 ' , 1) # digital replaced by 'H', the parameter 1 replaces only a 
Print (RET) # evaHegon4yuan4

 

subn method

= re.subn RET ( ' \ D ' , ' H ' , ' eva3egon4yuan4 ' ) # digital replaced by 'H', returns a tuple (a result of the replacement, the replacement how many times) 
Print (RET)

 

compile Method

= the re.compile obj ( ' \ D {}. 3 ' )   # regular expression compiled into a regular expression object, the rule is to be matched 3 digits 
RET = obj.search ( ' abc123eeee ' ) # regular expression object call search, the parameters to be matched string 
Print (ret.group ())   # results: 123

 

finditer method

= re.finditer RET ( ' \ D ' , ' ds3sy4784a ' )    # finditer returns an iterator stored matching results 
Print (RET)   # <Object callable_iterator AT 0x10195f940> 
Print (Next (RET) .group ())   # View of a results 
Print (Next (RET) .group ())   # View second result 
Print ([i.group () for I in RET])   # view about the remaining results

 

Guess you like

Origin www.cnblogs.com/hercules-chung/p/12388069.html