Python basic eleventh day -re module

reModular

common method

  • findall()

    Returns all eligible results in a list.

    1 import  re
    2 print(re.findall('\d','a1b2c2abc123'))#['1', '2', '2', '1', '2', '3']

    View Code

  • search()

    Finding the first matching information returns an object containing matching information (returning None if not found), which looks at the result through the group () function

    1 import re
    2 
    3 str = 'a1b2c2abc123'
    4 print(re.search('\d', str).group())  # 1

    View Code

  • match()

    Starting from string, location matching, others with search ()

    1 import re
    2 
    3 str = 'a1b2c2abc123'
    4 print(re.match('\d', str).group() if re.match('\d', str) else 'not found')  # not found
    5 print(re.match('a.+', str).group() if re.match('a', str) else 'not found')  # a1b2c2abc123

    View Code

  • split()

    Cuts the string with regular matching characters, and returns the list of cut strings.

    1 import re
    2 
    3 str = 'a1b2c2abc123'
    4 print(re.split('[123]', str))  # ['a', 'b', 'c', 'abc', '', '', '']

    View Code

  • sub()

    Replace the matching content with the specified content and return the replaced string ().

    1 import re
    2 
    3 str = 'a1b2c2abc123'
    4 print(re.sub('[123]', 'A', str))  # aAbAcAabcAAA

    View Code

  • subn()

    Replaces the matched content with the specified content and returns the meta-ancestor. The first value is the substituted string, and the second value is the number of substitutions.

    1 import re
    2 
    3 str = 'a1b2c2abc123'
    4 print(re.subn('[123]', 'A', str))  # ('aAbAcAabcAAA', 6)

    View Code

  • compile()

    Compiles regular expressions into bytecode objects, which can be called multiple times.

     1 import re
     2 
     3 str = 'a1b2c2abc123'
     4 re_obj = re.compile('[123]')
     5 print(re_obj.sub('A', str))  # aAbAcAabcAAA
     6 print(re_obj.subn('A', str))  # ('aAbAcAabcAAA', 6)
     7 print(re_obj.findall(str))  # ['1', '2', '2', '1', '2', '3']
     8 print(re_obj.split(str))  # ['a', 'b', 'c', 'abc', '', '', '']
     9 print(re_obj.match(str))  # None
    10 print(re_obj.search(str).group())  # 1

    View Code

  • finditer()

    Returns an iterator that matches the result.

    1 import re
    2 
    3 str = 'a1b2c2abc123'
    4 result = re.finditer('\d', str)
    5 print(result)  # <callable_iterator object at 0x00000000026F80F0>
    6 print(next(result).group())  # 1
    7 print(next(result).group())  # 2
    8 print([g.group() for g in result])  # ['2', '1', '2', '3']

    View Code

Priority problem

  • findall()

    findallThe result of group matching results is preferentially returned.

    ?: You can cancel the matching result of priority return group.

    1 import re
    2 
    3 print(re.findall('zz([a-y])', 'zze'))  # ['e']
    4 print(re.findall('zz(?:[a-y])', 'zze'))  # ['zze']

    View Code

  • split()

    When regular inclusion groups are set, the segmentation result is preserved.

    1 import re
    2 
    3 str = 'a1b2c2abc123'
    4 print(re.split('\d', str))  # ['a', 'b', 'c', 'abc', '', '', '']
    5 print(re.split('(\d)', str))  # ['a', '1', 'b', '2', 'c', '2', 'abc', '1', '', '2', '', '3', '']

    View Code

extend

  • Group naming

    Can be named by group P’?

    1 import re
    2 
    3 str = '<title>python learning</title>'
    4 result = re.search('<(?P<tag_name>[a-z]+)>(?P<title>.+)</([a-z]+)>', str)
    5 print(result.group('title'))  # python learning
    6 print(result.group('tag_name'))  # title
    7 print(result.group())  # <title>python learning</title>

    View Code

Leave a Reply

Your email address will not be published. Required fields are marked *