当前位置:首页 > 编程语言 > 正文

Python3中正则模块re.compile、re.match及re.search函数

11-19 编程语言

标签:unicode   comm   函数   tps   div   提高效率   方式   ocs   通过   

参考:https://www.jb51.net/article/141830.htm

官网:https://docs.python.org/3/library/re.html

 

re.compile() 函数

编译正则表达式模式,返回一个对象。可以把常用的正则表达式编译成正则表达式对象,方便后续调用及提高效率。

re.compile(pattern, flags=0)

    pattern 指定编译时的表达式字符串
    flags 编译标志位,用来修改正则表达式的匹配方式。支持 re.L|re.M 同时匹配

flags 标志位参数

re.I(re.IGNORECASE)
使匹配对大小写不敏感

re.L(re.LOCAL) 
做本地化识别(locale-aware)匹配

re.M(re.MULTILINE) 
多行匹配,影响 ^ 和 $

re.S(re.DOTALL)
使 . 匹配包括换行在内的所有字符

re.U(re.UNICODE)
根据Unicode字符集解析字符。这个标志影响 w, W, b, B.

re.X(re.VERBOSE)
该标志通过给予你更灵活的格式以便你将正则表达式写得更易于理解。

 

在python 3.7版本==》 pattern= re.compile(‘<psclass="s2">(.*?)</p>‘,RegexFlag.S)

__version__ = "2.2.1"

class RegexFlag(enum.IntFlag):
    ASCII = sre_compile.SRE_FLAG_ASCII # assume ascii "locale"
    IGNORECASE = sre_compile.SRE_FLAG_IGNORECASE # ignore case
    LOCALE = sre_compile.SRE_FLAG_LOCALE # assume current 8-bit locale
    UNICODE = sre_compile.SRE_FLAG_UNICODE # assume unicode "locale"
    MULTILINE = sre_compile.SRE_FLAG_MULTILINE # make anchors look for newline
    DOTALL = sre_compile.SRE_FLAG_DOTALL # make dot match newline
    VERBOSE = sre_compile.SRE_FLAG_VERBOSE # ignore whitespace and comments
    A = ASCII
    I = IGNORECASE
    L = LOCALE
    U = UNICODE
    M = MULTILINE
    S = DOTALL
    X = VERBOSE
    # sre extensions (experimental, dont rely on these)
    TEMPLATE = sre_compile.SRE_FLAG_TEMPLATE # disable backtracking
    T = TEMPLATE
    DEBUG = sre_compile.SRE_FLAG_DEBUG # dump pattern after compilation
globals().update(RegexFlag.__members__)

# sre exception
error = sre_compile.error

 

 

 

 

 

import re
content = Citizen wang , always fall in love with neighbour,WANG
rr = re.compile(rwanw, re.I) # 不区分大小写
print(type(rr))
a = rr.findall(content)
print(type(a))
print(a)

 

Python3中正则模块re.compile、re.match及re.search函数

标签:unicode   comm   函数   tps   div   提高效率   方式   ocs   通过   

温馨提示: 本文由杰米博客推荐,转载请保留链接: https://www.jmwww.net/file/biancheng/12301.html