zoukankan      html  css  js  c++  java
  • 2正则表达式的预编译

    """正则表达式的预编译"""


    """
    在Python中使用正则表达式,正则表达式会首先被编译。如果一个正则表达式要重复多次使用,可以对其进行预编译,这样,后面的使用就不需要再编译了。
    compile(pattern, flags=0):
    "Compile a regular expression pattern, returning a Pattern object."
    其中:
    flags是一个可选标志,与match()方法可选标志含义相同
    compile 函数用于编译正则表达式,生成一个正则表达式( Pattern )对象,供 match() 和 search() 这两个函数使用.

    除了直接调用模块re的方法match()外,也可以调用模块re的方法compile()的返回值的方法:
    match(string[, pos[, endpos]])
    其中:
    pos用于指定被匹配字符串的起始位置,默认是0
    endpos用于指定被匹配字符串的结束位置,默认是字符串的长度。被匹配的子串不包括结束位置。
    """

    import re

    pattern_obj = re.compile(r'...')
    print(pattern_obj) # re.compile('...')

    # <re.Match object; span=(0, 3), match='abc'>
    # print(re.match(r'...', 'abcdefg'))
    print(re.compile(r'...').match('abcdefg'))
    # None
    print(re.compile(r'...').match('abcdefg', 1, 3))
    # <re.Match object; span=(1, 4), match='bcd'>
    print(re.compile(r'...').match('abcdefg', 1, 4))
  • 相关阅读:
    HDOJ 4747 Mex
    HDU 1203 I NEED A OFFER!
    HDU 2616 Kill the monster
    HDU 3496 Watch The Movie
    Codeforces 347A A. Difference Row
    Codeforces 347B B. Fixed Points
    Codeforces 372B B. Hungry Sequence
    HDU 1476 Sudoku Killer
    HDU 1987 How many ways
    HDU 2564 词组缩写
  • 原文地址:https://www.cnblogs.com/sruzzg/p/12989432.html
Copyright © 2011-2022 走看看