Python Module-re module

Source: Internet
Author: User

Re is matched, if no match is returned to none, the match result plus. Group () returns the matching result as a string

    • The matching syntax for re

Re.match () match from the beginning

>>> re.match (' ab ', ' ABCDEFG ') <_sre. Sre_match object; span= (0, 2), match= ' ab ' >>>> re.match (' ab ', ' CABDEFG ')

Match only from the first one on the left

Re.search () match contains

>>> re.search (' ab ', ' ABCDEFG ') <_sre. Sre_match object; span= (0, 2), match= ' ab ' >>>> re.search (' ab ', ' CABDEFG ') <_sre. Sre_match object; Span= (1, 3), match= ' AB ' >

Start a match from anywhere in the string

The Re.match () and Re.search () methods can only match once, and if you want to match all the results, use the Re.findall ()

Re.findall () put all matching characters into the list

>>> re.findall (' ab ', ' abcdefabcdefab ') [' AB ', ' ab ', ' AB ']

Re.split () separates elements with matching characters as list separators

>>> re.split (' \+ ', ' ab+c+d+ef+gh ') [' AB ', ' C ', ' d ', ' ef ', ' gh ']

Because "+" is a matching rule, you need to escape the "+" with the escape character as a normal "+"

Re.sub () match characters and replace

>>> re.sub (' \. ', ' 5 ', ' org.cn.net.com.gov ') ' org5cn5net5com5gov ' >>> re.sub ('. ', ' 5 ', ' Org.cn.net.com.gov ', count=2) ' org5cn5net.com.gov '

Count is the number of replacements

Re.fullmatch () match all

>>> re.fullmatch (' python ', ' python ') <_sre. Sre_match object; span= (0, 6), match= ' Python ' >

To exactly match the entire string

Re.compile (pattern, flags=0) matching rules

>>> test = Re.compile (' [th]+ ', Flags=re. I) >>> test.search (' Python '). Group () ' th '
    • Common expression rules for re

‘.‘ The default match is any character except \ n

>>> re.match ('. ', ' python ') <_sre. Sre_match object; span= (0, 1), match= ' P ' >>>> re.match ('. ', ' Java ') <_sre. Sre_match object; span= (0, 1), match= ' J ' >>>> re.findall ('. ', ' Ja\nva ') [' J ', ' a ', ' V ', ' a ']

' ^ ' and ' \a ' begin matching from the beginning of the string

>>> re.match (' P ', ' Python ') <_sre. Sre_match object; span= (0, 1), match= ' P ' >>>> re.search (' ^p ', ' Python ') <_sre. Sre_match object; span= (0, 1), match= ' P ' >>>> re.search (' \ap ', ' Python ') <_sre. Sre_match object; span= (0, 1), match= ' P ' >

The above three matches are only the first to start matching from the left

' $ ' and ' \z ' start matching from the end of the string

>>> re.search (' on$ ', ' Python ') <_sre. Sre_match object; Span= (4, 6), match= ' on ' >>>> re.search (' on\z ', ' Python ') <_sre. Sre_match object; Span= (4, 6), match= ' on ' >

' * ' matches the character preceding the * number 0 or more times

>>> re.search (' ab* ', ' ABBBCABBCD ') <_sre. Sre_match object; Span= (0, 4), match= ' abbb ' >>>> re.search (' (AB) * ', ' ABABABABCABBCD ') <_sre. Sre_match object; span= (0, 8), match= ' Abababab ' >>>> re.search (' f* ', ' ABABABABCABBCD ') <_sre. Sre_match object; span= (0, 0), match= ' >

F did not match to, returned empty, not none

' + ' matches a previous character 1 or more times

>>> re.search (' ab+ ', ' ABBBCABBCD ') <_sre. Sre_match object; Span= (0, 4), match= ' abbb ' >>>> re.search (' (AB) + ', ' ABABABABCABBCD ') <_sre. Sre_match object; span= (0, 8), match= ' Abababab ' >>>> re.search (' f+ ', ' ABABABABCABBCD ')

F no match to, so return none

‘?‘ Match a previous character 1 or 0 times

>>> re.search (' ab ', ' ABBBBC ') <_sre. Sre_match object; span= (0, 2), match= ' ab ' >>>> re.search (' d? ', ' ABBBBC ') <_sre. Sre_match object; span= (0, 0), match= ' >

‘[...]‘ matches the characters in [], [0-9] is a number, [A-z] is all lowercase letters, [A-z] is all uppercase letters, [a-za-z0-9] is a full-size uppercase and numeric

>>> Re.findall (' [PTO] ', ' python ') [' P ', ' t ', ' O ']>>> re.findall (' [A-z] ', ' python ') [' P ', ' y ', ' t ', ' H ', ' O ', ' n ']>>> re.findall (' [A-z] ', ' PyThoN ') [' P ', ' T ', ' n ']>>> re.findall (' [0-9] ', ' sch01ar ') [' 0 ', ' 1 ' ]>>> Re.findall (' [a-za-z0-9] ', ' [email protected]# ') [' P ', ' y ', ' T ', ' h ', ' 0 ', ' n ']

' [^ ...] ' Match characters that are not in []

>>> Re.findall (' [^pto] ', ' python ') [' Y ', ' h ', ' N ']>>> re.findall (' [^a-z] ', ' python ') [' Y ', ' h ', ' O '] >>> Re.findall (' [^a-z] ', ' PyThoN ') [' P ', ' T ', ' N ']>>> re.findall (' [^0-9] ', ' sch01ar ') [' s ', ' C ', ' H ', ' A ', ' R ']>>> re.findall (' [^a-za-z0-9] ', ' sch01ar ') []

' {m} ' matches the previous character m times

>>> re.search (' [0-9]{3} ', ' 12345 ') <_sre. Sre_match object; Span= (0, 3), match= ' 123 ' >

Match Number 3 times

' {n,m} ' matches the previous character N to M times

>>> Re.findall (' [0-9]{2,4} ', ' abc1abcd12ab123abcde1234abcdef12345abc ') [' 12 ', ' 123 ', ' 1234 ', ' 1234 ']

Match 2 to 4 digits of 0-9 digits

| Match | left or | right character

>>> re.findall (' h|n ', ' pythonandphp ') [' H ', ' n ', ' n ', ' H ']>>> re.findall (' h|b ', ' pythonandphp ') [' H ', ' H ']>>> re.findall (' b|h ', ' pythonandphp ') [' H ', ' H ']>>> re.search ("abc| ABC "," ABCBABCCD "). Group () ' ABC '

| Right priority on left

‘(...)‘ Group Matching

>>> Re.search ("(ABC) {2}A (123|45)", "abcabca456c"). Group () ' Abcabca45 ' >>> re.search ("(ABC) {2}A ( 123|45) "," abcabca456c "). Groups (' abc ', ' 45 ')

Groups () returned as a tuple

‘(?   P<name>, ...) ' Group Matching

>>> Re.search (? P<year>[0-9]{4}) (? P<month>[0-9]{2}) (? P<DAY>[0-9]{2}) ", ' 19930519 '). Groupdict () {' Year ': ' 1993 ', ' Day ': ' + ', ' month ': ' 05 '}

Groupdict () returns in dictionary form

' \d ' matches the number 0-9

>>> Re.findall ("\d", "abc123abc456abc") [' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ']>>> re.findall ("\d+", " Abc123abc456abc ") [' 123 ', ' 456 ']

' \d ' matches non-numeric

>>> Re.findall ("\d+", "abc123abc456abc") [' abc ', ' abc ', ' ABC ']

' \w ' match [a-za-z0-9]

>>> Re.findall ("\w+", "ab-c12*3abc#456") [' ab ', ' C12 ', ' 3abc ', ' 456 ']

' \w ' matches non-[a-za-z0-9], can also match whitespace characters, \ t, \ n, \ r

>>> Re.findall ("\w+", "ab-c12*3abc#456") ['-', ' * ', ' # ']

' \s ' matches whitespace characters, \ t, \ n, \ r

>>> Re.findall ("\s+", "AB BC \ndef\twed\r123") [', ' \ n ', ' \ t ', ' \ R ']

' \s ' matches non-whitespace characters, \ t, \ n, \ r

>>> Re.findall ("\s+", "AB BC \ndef\twed\r123") [' AB ', ' BC ', ' def ', ' Wed ', ' 123 ']
    • Flags flag bit

Flags flag, used to control how regular expressions are matched

In parentheses is a complete notation

I (IGNORECASE): Ignore case

>>> Re.findall ("[A-Z]", "Sch01ar", re. I) [' S ', ' C ', ' h ', ' A ', ' R ']>>> re.findall ("[A-Z]", "Sch01ar", re. IGNORECASE) [' S ', ' C ', ' h ', ' A ', ' R ']

M (MULTILINE): Multiline mode, change ' ^ ' and ' $ ' to match opening end behavior

>>> re.search (' [ef]+ ', ' Abc\nefg\nhij ', flags=re. M). Group () ' EF ' >>> re.search (' [ef]+ ', ' Abc\nefg\nhij ', flags=re. MULTILINE). Group () ' EF '

S (dotall): Change '. ' Behavior, can also match \ n

>>> Re.search ('. + ', ' abc\nefg\nhij ', flags=re. S). Group () ' Abc\nefg\nhij ' >>> re.search ('. + ', ' abc\nefg\nhij ', flags=re. Dotall). Group () ' Abc\nefg\nhij '

X (VERBOSE): Add a comment to the matching rule

>>> re.search (' [0-9]+ #匹配一个或多个0-9 number ', ' abc123cde456fgh ', re. X). Group () ' 123 ' >>> re.search (' [0-9]+ #匹配一个或多个0-9 number ', ' abc123cde456fgh ', re. VERBOSE). Group () ' 123 '

Python Module-re module

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.