1. If import RE is introduced
Matching mailbox suffix requires writing r=r ' \.com\.cn|\.com|\.cn '
r=R ' (\[email protected]\w+ (\.com\.con|\.com|\.cn)) '
Ll=re.findall (R,"[email protected]")
Print (LL)
2. Regular common function compile
www=R ' a[bcd]c '
P=re.compile (www)
Jj=p.findall ("ABCD,ADC,AAC,ACC")
Print (JJ)
3. When matching with match and search, be sure to pay attention to the parentheses grouping
r=R ' (a[bcd]e) '
Ll=re.match (R,"Ace,ade,afe,age,abe")
Print (Ll.group ())
Match matches only the beginning
r=R ' (a[bcd]e) '
Ll=re.search (R,"Afe,ade,afe,age,abe")
Print (Ll.group ())
Search matches the full text, but it is the first content that is returned to a match
strw="Welcome to Python World"
R=R ' (. *) to (. *?). *‘
Ll=re.match (R, strw)
if ll:
Print (Ll.group (0), 1) #匹配整个字符串表达式
Print (Ll.group (1), 2)
print (Ll.group (3)
print (Ll.groups (), 4)
print ( Ll.group (1,5)
4. Advanced methods of Use:
C=re.sub (R "A.. D ","Python","ADFD,AFFD,AOOD,QAPPD")
PrintC
C=re.sub (R "A.. D ",' Python ', Addd,adfd,afdf,eealld ', 2) #后面的2是匹配次数
print ( C)
c=re.subn (r ' A.. d ', ' Python ', ' AFDD,ADDD,AWWD,AVVD ')
print ( C) #返回一个元组, that's a number more than a sub.
c=re.split (r ' [\+\-\*\/] ', Span style= "color: #008000; Font-weight:bold; " > "1+2*3/4")
print ( C) #按正则进行切割
Python chapter 12th Regular Expressions (2)