zoukankan      html  css  js  c++  java
  • 3.2.3 正則表達式对象

    编译之后的正則表達式对象支持的方法和属性例如以下:

    regex.search(string[, pos[, endpos]])

    从字符串string的開始位置pos開始匹配正則表達式。到位置endpos结束匹配。匹配成功返回match对象。否则返回None

    括号里的參数表示可选。比方rx.search(string, 0, 50)等价于rx.search(string[:50], 0)

    样例:

    print('regex.search()')

    pattern = re.compile('c')

    print(pattern.search('caimouse'))

    print(pattern.search('caimouse', 1))

    结果输出例如以下:

    regex.search()

    <_sre.SRE_Match object; span=(0, 1), match='c'>

    None

    regex.match(string[, pos[, endpos]])

    指定从字符串string头部或者指定位置的头部匹配。參数pos默认值为0。表示从字符串指定位置開始进行匹配。參数endpos匹配结束位置。

    假设搜索字符串任何位置全部字符串,须要使用regex.search()方法。

    样例:

    print('regex.match()')

    pattern = re.compile('c')

    print(pattern.match('caimouse'))

    print(pattern.match('caimouse', 1))

    结果输出例如以下:

    regex.match()

    <_sre.SRE_Match object; span=(0, 1), match='c'>

    None

    regex.fullmatch(string[, pos, endpos]])

    当整个string与正則表達式匹配时返回match对象,否则返回None。參数posendpossearch()的意义一样。

    样例:

    print('regex.fullmatch()')

    pattern = re.compile('c[ai]')

    print(pattern.fullmatch('caimouse'))

    print(pattern.fullmatch('caimouse', 0,2))

    结果输出例如以下:

    regex.fullmatch()

    None

    <_sre.SRE_Match object; span=(0, 2), match='ca'>

    regex.split(string, maxsplit=0)

    本方法与re.split()一样。

    regex.findall(string[, pos[, endpos]])

    re.findall()一样。本方法接收參数posendpos參数,能够指定開始位置和结束位置。

    regex.finditer(string[, pos[, endpos]])

    re.finditer()一样。本方法接收參数posendpos參数。能够指定開始位置和结束位置。

    regex.sub(repl, string, count=0)

    re.sub()一样。

    regex.subn(repl, string, count=0)

    re.subn()一样。

    regex.flags

    regex匹配的标志。

    样例:

    print('regex.flags')

    pattern = re.compile('c[ai]')

    print(pattern.fullmatch('caimouse'))

    print(pattern.flags)

    pattern = re.compile('c[ai]', re.ASCII)

    print(pattern.flags)

    结果输出例如以下:

    regex.flags

    None

    32

    256

    regex.groups

    正則表達式匹配分组的数量。

    样例:

    print('regex.groups')

    pattern = re.compile('(?

    P<style>[^|]*)|(?P<tags>[^|]*)')

    print(pattern.findall('OL|AAAAA'))

    print(pattern.groups)

    结果输出例如以下:

    regex.groups

    [('OL', 'AAAAA')]

    2

    regex.groupindex

    返回分组的名称和序号,以字典方式返回。

    假设没有返回空字典。

    样例:

    print('regex.groups')

    pattern = re.compile('(?P<style>[^|]*)|(?P<tags>[^|]*)')

    print(pattern.findall('OL|AAAAA'))

    print(pattern.groups)

    print(pattern.groupindex)

    结果输出例如以下:

    regex.groups

    [('OL', 'AAAAA')]

    2

    {'style': 1, 'tags': 2}

    regex.pattern

    已经编译的正則表達式的字符串。

    样例:

    print('regex.pattern')

    pattern = re.compile('(?P<style>[^|]*)|(?P<tags>[^|]*)')

    print(pattern.findall('OL|AAAAA'))

    print(pattern.pattern)

    结果输出例如以下:

    regex.pattern

    [('OL', 'AAAAA')]

    (?P<style>[^|]*)|(?P<tags>[^|]*)


    蔡军生  QQ:9073204  深圳

  • 相关阅读:
    ZOJ Problem Set–2417 Lowest Bit
    ZOJ Problem Set–1402 Magnificent Meatballs
    ZOJ Problem Set–1292 Integer Inquiry
    ZOJ Problem Set–1109 Language of FatMouse
    ZOJ Problem Set–1295 Reverse Text
    ZOJ Problem Set–1712 Skew Binary
    ZOJ Problem Set–1151 Word Reversal
    ZOJ Problem Set–1494 Climbing Worm
    ZOJ Problem Set–1251 Box of Bricks
    ZOJ Problem Set–1205 Martian Addition
  • 原文地址:https://www.cnblogs.com/brucemengbm/p/6915028.html
Copyright © 2011-2022 走看看