Wu Yuxiong - born naturally PYTHON3 development of learning: Regular Expressions

Import Re
 Print (re.match ( ' WWW ' , ' www.runoob.com ' ) .span ())   # match in the starting position 
Print (re.match ( ' COM ' , ' www.runoob.com ' ))          # not match the starting position
Import Re 
 
Line = " Cats Dogs are Smarter Within last " 
# . * represents any other matching newline (\ n, \ r) than any single or plurality of characters 
matchObj = re.match (R & lt ' (. *) are ( . *) *?. ' , Line, re.M | re.I) 
 
IF matchObj:
    Print ( " matchObj.group (): " , matchObj.group ())
    Print ( " matchObj.group (1): " , matchObj.group (. 1 ))
    Print ( " matchObj.group (2): " , matchObj.group (2 ))
 the else :
    Print ("No match!!")
Import Re 
 
Print (the re.search ( ' WWW ' , ' www.runoob.com ' ) .span ())   # match in the starting position 
Print (the re.search ( ' COM ' , ' www.runoob.com ' ). span ())          # does not match the start position
import re
 
line = "Cats are smarter than dogs";
 
searchObj = re.search( r'(.*) are (.*?) .*', line, re.M|re.I)
 
if searchObj:
   print ("searchObj.group() : ", searchObj.group())
   print ("searchObj.group(1) : ", searchObj.group(1))
   print ("searchObj.group(2) : ", searchObj.group(2))
else:
   print ("Nothing found!!")
import re
 
line = "Cats are smarter than dogs";
 
matchObj = re.match( r'dogs', line, re.M|re.I)
if matchObj:
   print ("match --> matchObj.group() : ", matchObj.group())
else:
   print ("No match!!")
 
matchObj = re.search( r'dogs', line, re.M|re.I)
if matchObj:
   print ("search --> matchObj.group() : ", matchObj.group())
else:
   print ("No match!!")
Import Re 
 
Phone = " 2004-959-559 # This is a phone number " 
 
# delete comments 
NUM = re.sub (r ' # * $. ' , '' , Phone)
 Print ( " Phone number: " , NUM) 
 
# removing non-numeric contents 
NUM = the re.sub (R & lt ' \ D ' , "" , phone)
 Print ( " phone number: " , NUM)
Import Re 
 
# digital matched multiplied by 2 
DEF Double (Matched): 
    value = int (matched.group ( ' value ' ))
     return STR (value * 2 ) 
 
S = ' A23G4HFD567 ' 
Print (the re.sub ( ' (? P <value> \ + D) ' , Double, S))
import re
 
pattern = re.compile(r'\d+')   # 查找数字
result1 = pattern.findall('runoob 123 google 456')
result2 = pattern.findall('run88oob123google456', 0, 10)
 
print(result1)
print(result2)
import re
 
it = re.finditer(r"\d+","12a32bc43jf3") 
for match in it: 
    print (match.group() )
. In addition to matching " \ the n- " any single character outside. To match include ' \ n- ' including any character, like the use of ' [. \ N-] ' mode. 
\ d Matches a digit character. It is equivalent to [0 -9 ]. 
\ D Matches a non-numeric characters. It is equivalent to [ ^ 0-9 ]. 
\ s Matches any whitespace characters, including spaces, tabs, page breaks, and so on. Is equivalent to [\ f \ n \ r \ t \ v]. 
\ S Matches any non-whitespace characters. Is equivalent to [ ^ \ F \ n-\ R & lt \ T \ V]. 
\ w matches any word character including underscore. It is equivalent to the ' [A-Za-z0-9_] ' . 
\ W matches any non-word character. It is equivalent to the ' [^ A-Za-z0-9_] ' .

 

Guess you like

Origin www.cnblogs.com/tszr/p/10963446.html