Python re module common functions

Source: Internet
Author: User

Re.match () function

If you want to match a pattern from the starting position of the source string, we can use the Re.match () function. The use format for the Re.match () function is:
Re.match (pattern, string, flag)

Re.search () function

We can also use the Re.search () function to match, use this function to match, scan the entire string and make corresponding matches.
The most significant difference between this function and the Re.match () function is that the Re.match () function matches from the beginning of the source string, and the Re.search () function retrieves a match in the full text.
Examples are as follows:

import repattern1 = "python"string = "abcdpythonfphp345pythonxadi_py"result1 = re.search(pattern1, string)print(result1)print(result1.group())

Execution Result:

<_sre.SRE_Match object; span=(4, 10), match=‘python‘>python
Re.compile ()

In the above two functions, even if more than one result in the source string matches the pattern, it will only match one result, so how do we match the content of the pattern in all?

    1. Use Re.compile () to precompile a regular expression.
    2. After compiling, use FindAll () to find all the matching results from the source string based on the regular expression.

We can better understand this through the following examples:

import restring = "hellomypythonhispythonourpythonend"pattern = re.compile(".python.")#预编译result = pattern.findall(string)#找出符合模式的所有结果print(result)

Execution Result:

[‘ypythonh‘, ‘spythono‘, ‘rpythone‘]

As you can see, this code outputs all of the results in string that satisfy the pattern pattern, with a total of 3 results that match the criteria.

Re.sub () function

If you want to implement the ability to replace certain strings based on regular expressions, we can use the Re.sub () function to implement them.
Using Re.sub, this function finds the result of conforming pattern from the source string string and replaces it with the string rep, up to Max times, according to the formal expression pattern.
The format of the Re.sub () function is as follows:
Re.sub (Pattern,rep,string,max)
Where the first parameter is the corresponding regular expression, the second argument is the string to be replaced, the third argument is the source string, the fourth argument is optional, represents the maximum number of replacements, and if omitted, the result of the pattern will be replaced by all.

import restring = "hellomypythonhispythonourpythonend"pattern = "python."result1 = re.sub(pattern,"php",string)   # 全部替换result2 = re.sub(pattern,"php",string,2) # 最多替换2次print(result1)print(result2)

The results are as follows:

hellomyphpisphpurphpndhellomyphpisphpurpythonend

The first line of output, because no fourth parameter is set, replace all.

Python re module common functions

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.