Python_re regular module

Regular module
  • The method comprising the matching result or attribute sre.Match

    Methods (properties) meaning
    match.group(index) Gets the string specified group
    match.__getitem__(g) That match [0] = match.group (0)
    match.groups() Get all of the group results
    match.span(index) Gets the string matches the string in the index section taken
    match.start(groupindex) To obtain the starting position of the specified group substring
    match.stop(groupindex) Gets the end position of the specified group substring
    match.re Return re object model comprising
    match.string Return the original string
    match.pos pos parameter returns the object passed to the regular expression search (), match () method and the like
method:
  • re.compile(p, flag=0)

    String (str) -> Regular objects (_sre.SRE_Pattern)
    such a program needs to be used multiple times with a regular string, you can consider in order to improve the efficiency of the compiler

    p = re.compile("baidu")                 # 编译正则对象p
    p.search("www.baidu.com")               # 效率高
    re.search("du", "www.baidu.com")        # 效率低
  • re.match(p, string, flag=0)

    Detecting whether the beginning of the match
    successful return re.Match object is returned unsuccessful None

  • re.fullmatch(p, string, flag=0)

    Detecting whether p and string matching full
    return _sre the object, otherwise None

  • re.search(p, string, flag=0)

    Scanning the first substring matching the search string
    successful return re.Match object unsuccessful returns None, matched to the first stop

  • re.findall(p, string, flag=0)

    Find full text matching substring all
    return match to the list of sub-strings

  • re.finditer (p string flag = 0)

    Iterator type of findall

  • re.sub(old, new, string, flag=0)

    After the full-text search p replaced with new

  • re.split (delimiter, string, maxsplit = 0, flag = 0)

    Divided
    by delimiter string into the list type, maxsplit specified number of division

  • *re.purge()

    Clear Cache Regular Expression

  • re.escape(pattern)

    In addition to the ASCII, numerical model, characters _ escape outside, for example:

    re.escape(r'www.baidu.com is good, i love')
    # 'www\\.baidu\\.com\\ is\\ good,\\ i\\ love'

Guess you like

Origin www.cnblogs.com/yuandongxu/p/11728040.html