10 matching methods for Python Regular Expressions

Source: Internet
Author: User

Python Regular Expressions require a variety of matching methods, but we cannot blindly match them. Next we will introduce ten matching methods for Python regular expressions that we often encounter. I hope you will gain some benefits.

1. test whether the Python Regular Expression matches all or part of the string.

 
 
  1. Regex = ur "..." # Regular Expression
  2. If re. search (regex, subject ):
  3. Do_something ()
  4. Else:
  5. Do_anotherthing ()

2. test whether the Python Regular Expression matches the entire string.

 
 
  1. Regex = ur "... \ Z" # The regular expression ends with \ Z.
  2. If re. match (regex, subject ):
  3. Do_something ()
  4. Else:
  5. Do_anotherthing ()

3. Create a matching object and obtain matching details through the object.

 
 
  1. Regex = ur "..." # Regular Expression
  2. Match = re. search (regex, 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 ()

4. Obtain the substring matched by the Python Regular Expression

 
 
  1. Regex = ur "..." # Regular Expression
  2. Match = re. search (regex, subject)
  3. If match:
  4. Result = match. group ()
  5. Else:
  6. Result = ""

5. Obtain the substring matching the capture group.

 
 
  1. Regex = ur "..." # Regular Expression
  2. Match = re. search (regex, subject)
  3. If match:
  4. Result = match. group (1)
  5. Else:
  6. Result = ""

6. Obtain the substring matched by the famous group

 
 
  1. Regex = ur "..." # Regular Expression
  2. Match = re. search (regex, subject)
  3. If match:
  4. Result = match. group ("groupname ")
  5. Else:
  6. Result = ""

7. Put all matched substrings in the string into the array.

 
 
  1. reresult = re.findall(regex, subject) 

8. traverse all matched substrings

 
 
  1. (Iterate over all matches in a string)  
  2. for match in re.finditer(r"<(.*?)\s*.*?/\1>", subject)  
  3. # match start: match.start()  
  4. # match end (exclusive): match.end()  
  5. # matched text: match.group() 

9. Create a regular expression object using a Python Regular Expression string

 
 
  1. (Create an object to use the same regex for many 
    operations)  
  2. rereobj = re.compile(regex) 

10. Python Regular Expression object version in use 1

 
 
  1. rereobj = re.compile(regex)  
  2. if reobj.search(subject):  
  3. do_something()  
  4. else:  
  5. do_anotherthing() 

The above is a solution for matching problems related to Python regular expressions.

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.