How to complete matching details in Python matching

Source: Internet
Author: User

Python matching has many precautions when we use it. We will encounter many problems during continuous learning. Next we will take a detailed look at how to better master the related Python matching technical issues. Regular Expression object version

 
 
  1. Rereobj = re. compile (r "\ Z") # End with \ Z at the end of the regular expression
  2. If reobj. match (subject ):
  3. Do_something ()
  4. Else:
  5. Do_anotherthing ()

Create a regular expression object and obtain Python matching details through this object.

 
 
  1. rereobj = re.compile(regex)  
  2. match = reobj.search(subject)  
  3. if match:  
  4. # match start: match.start()  
  5. # match end (exclusive): match.end()  
  6. # matched text: match.group()  
  7. do_something()  
  8. else:  
  9. do_anotherthing() 

Obtain a Python matched substring using a regular expression object

 
 
  1. rereobj = re.compile(regex)  
  2. match = reobj.search(subject)  
  3. if match:  
  4. result = match.group()  
  5. else:  
  6. result = "" 

Obtain the Python-matched substring of the capture group using the regular expression object

 
 
  1. rereobj = re.compile(regex)  
  2. match = reobj.search(subject)  
  3. if match:  
  4. result = match.group(1)  
  5. else:  
  6. result = "" 

Use a regular expression object to obtain the Python-matched substring of a famous group

 
 
  1. rereobj = re.compile(regex)  
  2. match = reobj.search(subject)  
  3. if match:  
  4. result = match.group("groupname")  
  5. else:  
  6. result = "" 

Use a regular expression object to obtain all Python matched substrings and put them in an array

 
 
  1. rereobj = re.compile(regex)  
  2. result = reobj.findall(subject) 

Traverse all Python matched substrings by using a regular expression object

 
 
  1. rereobj = re.compile(regex)  
  2. for match in reobj.finditer(subject):  
  3. # match start: match.start()  
  4. # match end (exclusive): match.end()  
  5. # matched text: match.group 

The above is a detailed introduction to Python matching.

Related Article

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.