The Python module learns the re regular expression.

Source: Internet
Author: User
Today I learned about regular expressions in Python. I have not explained much about the regular expression syntax. I have learned a lot on the Internet. This section describes the re. match function, a common regular expression processing function in Python.
Re. match tries to match a pattern from the beginning of the string. for example, the following example matches the first word.

The code is as follows:


Import re
Text = "JGood is a handsome boy, he is cool, clever, and so on ..."
M = re. match (r "(\ w +) \ s", text)
If m:
Print m. group (0), '\ n', m. group (1)
Else:
Print 'not match'


Re. match function prototype: re. match (pattern, string, flags)
The first parameter is a regular expression. here it is "(\ w +) \ s". if the Match is successful, a Match is returned; otherwise, a None is returned;
The second parameter indicates the string to be matched;
The third parameter is the Peugeot bit, which is used to control the matching mode of regular expressions, such as case-sensitive or multi-line matching.
Re. search
The re. search function searches for the pattern match in the string, only to find the first match and then returns it. if the string does not match, None is returned.

The code is as follows:


Import re
Text = "JGood is a handsome boy, he is cool, clever, and so on ..."
M = re. search (r' \ shan (ds) ome \ S', text)
If m:
Print m. group (0), m. group (1)
Else:
Print 'not search'


Re. search function prototype: re. search (pattern, string, flags)
The meaning of each parameter is the same as that of re. match.
Re. match and re. search differences: re. match only matches the start of the string. if the start of the string does not match the regular expression, the match fails, and the function returns None. search matches the entire string until a match is found.
Re. sub
Re. sub is used to replace the match in the string. In the following example, replace space '''-':

The code is as follows:


Import re
Text = "JGood is a handsome boy, he is cool, clever, and so on ..."
Print re. sub (r' \ s + ','-', text)


Re. sub function prototype: re. sub (pattern, repl, string, count)
The second function is the replaced string. In this example, it is '-'
The fourth parameter indicates the number of replicas. The default value is 0, indicating that each matching item is replaced.
Re. sub also allows the use of functions to replace matching items for complex processing. For example, re. sub (r '\ S', lambda m:' ['+ m. group (0) + ']', text, 0); replace the space ''in the string with '[]'.
Re. split
You can use re. split to split a string, such as re. split (r '\ s +', text). the string is split into a word list by space.
Re. findall
Re. findall can obtain all matching strings in the string. For example, re. findall (r' \ w * oo \ w * ', text); obtains all words containing 'oo' in a string.
Re. compile
You can compile a regular expression into a regular expression object. You can compile regular expressions that are frequently used into regular expression objects to improve efficiency. The following is an example of a regular expression object:

The code is as follows:


Import re
Text = "JGood is a handsome boy, he is cool, clever, and so on ..."
Regex = re. compile (r' \ w * oo \ w *')
Print regex. findall (text) # search for all words containing 'oo'
Print regex. sub (lambda m: '[' + m. group (0) + ']', text) # enclose words containing 'oo 'in.


For more details, refer to the Python manual.

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.