Python Regular Expression re compile function parsing, pythoncompile

Source: Internet
Author: User

Python Regular Expression re compile function parsing, pythoncompile

The re Regular Expression module also includes some useful functions for operating regular expressions. The following describes the compile function.

Definition:

Compile (pattern [, flags]) creates a pattern object based on a string containing a regular expression.

Use the help function of python to view the meaning of compile:

help(re.compile)

Compile (pattern, flags = 0)

Compile a regular expression pattern, returning a pattern object.

With help, you can see the introduction of the compile method. A pattern object is returned, but the second parameter flags is not described. The second parameter flags is the matching mode. It can take effect by bit or '|', or it can be specified in a regular expression string. The Pattern object cannot be directly instantiated and can only be obtained through the compile method. Matching modes include:

1). re. I (re. IGNORECASE): case insensitive
2). re. M (MULTILINE): MULTILINE mode, changing the behavior of '^' and '$'
3). re. S (DOTALL): Any point matching mode, changing the behavior '.'
4). re. L (LOCALE): Make the pre-defined character class \ w \ W \ B \ B \ s \ S depends on the current region settings
5). re. U (UNICODE): Make the predefined character class \ w \ W \ B \ B \ s \ S \ d \ D depend on the character attribute defined by unicode
6). re. X (VERBOSE): VERBOSE mode. In this mode, the regular expression can be multiple rows, ignore blank characters, and add comments.

For example:

Pattern1 = re. compile (r "\ d + # integer part. # decimal point \ d * # decimal part", re. X)

Here, if the regular expression is a string of multiple rows caused by three "signs, the matching mode is set to re. X to allow multi-row matching.

The function re. compile converts a regular expression (written in strings) into a pattern object to achieve more effective matching. Example:

Import retext = "JGood is a handsome boy, he is cool, clever, and so on... "re. findall (R' \ w * oo \ w * ', text) # Find all words containing 'oo'

Use the compile function:

Import re module:

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) # Find all words containing 'oo'

Summary

The above is all about parsing the compile function of python Regular Expression re. I hope it will be helpful to you. For more information, see Python_LDA, python + mongodb data capture, and Python search path modification. If you have any shortcomings, please leave a message. Thank you for your support!

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.