Python regular matching-> modules and basic functions

Source: Internet
Author: User

python Regular expression-RE module


1. FindAll function

>>> Import re

>>> s= ' 123abc456eabc789 '

>>> Re.findall (R ' ABC ', s) results: [' abc ', ' ABC ']

The return result is a list with a rule-compliant string in the middle and an empty list if no strings that match the rules are found.


2. Compile function

>>> s= ' 111,222,aaa,bbb,ccc333,444ddd123abc456eabc789 '

>>> rule=r ' \b\d+\b '

>>> compiled_rule=re.compile (rule)

>>> Compiled_rule.findall (s) results: [' 111 ', ' 222 ']

Directly using findall (rule, target) to match the string, once two times nothing, if it is used many times, because the regular engine every time to explain the rules again, and the rules of interpretation is quite time-consuming, so the efficiency is very low. If you want to use the same rule more than once to match, you can use the Re.compile function to precompile the rule, using the compiled regular Expression object or the pattern object to find it.


The compile function can also specify some rule flags to specify some special options, with '|' between multiple options. (bit or) connected.

I IGNORECASE Ignore case differences

M MULTILINE multiple lines match. In this mode ' ^ ' (representing the beginning of the string) and ' $ ' (representing the end of the string) will be able to match multiple rows of cases, becoming the beginning and end of the line tag.

>>> s= ' 123 456\n789 012\n345 678 '

>>> rc=re.compile (R ' ^\d+ ') #匹配一个位于开头的数字, not using the M option

>>> Rc.findall (s)

[' 123 '] #结果只能找到位于第一个行首的 ' 123 '


>>> rcm=re.compile (R ' ^\d+ ', re. m) #使用 m option

>>> Rcm.findall (s)

[' 123 ', ' 789 ', ' 345 '] #找到了三个行首的数字


The case for ' $ ' is similar:

>>> rc=re.compile (R ' \d+$ ')

>>> rcm=re.compile (R ' \d+$ ', re. M

>>> Rc.findall (s)

[' 678 ']

>>> Rcm.findall (s)

[' 456 ', ' 012 ', ' 678 '] there are some other rule flags, reference links: http://blog.csdn.net/smilelance/article/details/6529950


3. Match function and search function

Match (rule, targetstring [, Flag])

Search (rule, targetstring [, Flag]) The first argument is regular, the second is the target string, and the third is the option (the same as the compile function)


Returns the result is not a list of strings, but rather a matchobject (if the match succeeds) by manipulating the matchobject to get more information; If the match is unsuccessful, they return a nonetype.

Therefore, the general rules of Use are:

>>> m=re.match (rule, target)

>>> if M: #必需先判断是否成功

DoSomething

The difference between two functions:

Match starts at the beginning of the string, and if the start position does not match successfully, it fails, and search skips the beginning and continues backwards looking for a matching string.


4. Other functions

Reference Link: http://blog.csdn.net/smilelance/article/details/6529950


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.