Python chapter 12th Regular Expressions (2)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.