Python uses regular expressions to replace successfully matched groups and outputs the number of replications. python Regular Expressions

Source: Internet
Author: User

Python uses regular expressions to replace successfully matched groups and outputs the number of replications. python Regular Expressions

A regular expression is a special character sequence that helps you easily check whether a string matches a certain pattern.

Python has added the re module since version 1.5. It provides the Perl-style regular expression mode.

The re module enables the Python language to have all the regular expression functions.

The compile function generates a regular expression object based on a mode string and optional flag parameters. This object has a series of methods for regular expression matching and replacement.

The re module also provides functions that are exactly the same as those of these methods. These functions use a pattern string as their first parameter.

This topic describes how to replace a matched group with a regular expression in python and how many replications are output.

Previously we learned how to limit the number of replicas. What if we want to know the number of replicas after the regular expression matches successfully? This is a good problem. In this case, we need to use another external function subn. This function outputs not only the replaced content, but also the number of replicas. For example:

# Python 3.6 # Cai junsheng # http://blog.csdn.net/caimouse/article/details/51749579#import rebold = re. compile (R' \ * {2 }(? P <bold_text> .*?) \ * {2} ') text = 'Make this ** cai **. this ** junsheng **. 'print ('text: ', Text) print ('bold:', Bold. subn (R' <B> \ g <bold_text> </B> ', text ))

The output is as follows:

Text: Make this **cai**. This **junsheng**.Bold: ('Make this <b>cai</b>. This <b>junsheng</b>.', 2)

Ps: There are several elements to explain about a regular expression.

If the python regular expression is captured and needs to be grouped, use this syntax (? P <Name> matched regular expression)
Re. compile is used to compile regular expressions and return objects.
P. finditer returns all matching iterators
P. sub transfers the matching item to the callback function and replaces the text with the return value.
M. groupdict. You can use the group name to obtain the corresponding value.

Summary

The above section describes how to replace a successfully matched group with a regular expression in python and how many replicas are output. I hope this will help you. If you have any questions, please leave a message, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!

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.