Matching methods of Python Regular Expressions

Source: Internet
Author: User

We often encounter a lot of difficulties in computer applications. For example, we will list Python Regular Expressions below. We will provide the following matching usage for problems that often occur in Python Regular Expressions, I hope you will have a better understanding of Python Regular Expressions in this article.

1. test whether the 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 regular expression matches the entire string.

 
 
  1. Regex= Ur"\ Z"# The End Of 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 the matching details through the object (Create an object with details about how the regex matches (part of) a string)

 
 
  1. Regex= Ur""# Regular Expression
  2. Match=Re. Search (regex, subject)
  3. If match:
  4. # Match start: match. start ()
  5. # Match end (exclusive): atch. end ()
  6. # Matched text: match. group ()
  7. Do_something ()
  8. Else:
  9. Do_anotherthing ()
  10.  

4. Get the substring matched by the regular expression (Get the part of a string matched by the regex)

 
 
  1. Regex= Ur""# Regular Expression
  2. Match=Re. Search (regex, subject)
  3. If match:
  4. Result=Match. Group ()
  5. Else:
  6. Result=""
  7.  

5. Get the part of a string matched by a capturing 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.  

6. Get the part of a string matched by a named group)

  1. How to Use Python script code in C ++
  2. Performance Comparison Between the Python programming language and Java
  3. Future Development Trend of the Python Programming Language
  4. Future Development Trend of the Python Programming Language
  5. How to unpack Python Sequences

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.