How to match a substring using a Python Regular Expression

Source: Internet
Author: User

Many Python Regular Expressions need to be matched with substrings, not only during replacement, but also in many places. Next we will learn in detail how to use a Python regular expression to obtain the desired matching substring.

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 = ""

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 (1)
  5. Else:
  6. Result = ""

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

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

Put all matched substrings in the string into the array (Get an array of all regex matches in a string)

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

Traverse all matched substrings (Iterate over all matches in a string)

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

Create a regular expression object (Create an object to use the same regex for each operations)

 
 
  1. rereobj = re.compile(regex) 

Python Regular Expression object version of use regex object for if/else branch whether (part of) a string can be matched)

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

Python Regular Expression object version 2 use regex object for if/else branch whether a string can be matched entirely)

 
 
  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 ()

The above is an introduction to the application of Python Regular Expressions in obtaining matched substrings.

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.