re模块 | Python 3.5

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

http://www.cnblogs.com/PythonHome/archive/2011/11/19/2255459.html

1.提供正则表达式相关操作

2.模式串和匹配串可以是Unicode或8-bit字符串,但不能混用

3.语法

  http://www.cnblogs.com/book-book/p/5411634.html

  '.' 在DOTALL模式下能匹配'\n'

  '^'、'$'在MULTILINE模式下能匹配多行起始和结束

  (?aiLmsux):

    A: ASCII-only

    I: ignore case

    L: local dependent

    M: multiple line

    S: dot all

    U: Unicode-only

    X: verbose

  (?P<name>...)

  (?P=name)

  (?#...)

  (?<=...)

  (?<!...)

  (?(id/name)yes-pattern|no-pattern)

  \A、\Z匹配字符串起始和结束位置

4.模块内容

  re.compile(pattern, flags=0)

    返回RegexObject对象

    flags有多个时用 '|' 隔开

      re.A  re.ASCII

      re.DEBUG

      re.I   re.IGNORECASE

      re.L   re.LOCAL

      re.M  re.MULTILINE

      re.S   re.DOTALL

      re.X   re.VERBOSE

  re.search(pattern, string, flags=0)

    在string中任意位置匹配,返回第一个匹配到的对象(match object),或返回None

  re.match(pattern, string, flags=0)

    在string起始位置匹配(即使有re.M属性)

  re.fullmatch(pattern, string, flags=0)

    当整个string能被pattern匹配时返回对应的match object,否则返回None

  re.split(pattern, string, maxsplit=0, flags=0)

    用pattern分割string,如果pattern是用括号括起来的,则能被pattern匹配的分割部分也将保留在结果中

    maxsplit不为0时,分割maxsplit次后剩余部分保留在结果中

    若开头(或结尾)能被pattern匹配,则结果列表中会以空串开始(或结束)

    目前: 空pattern将会被忽略,且若pattern可能为空,有FutureWarning,若pattern为空,有ValueError

  re.findall(pattern, string, flags=0)

    找出所有不重叠的匹配子串并返回一个列表

  re.finditer(pattern, string, flags=0)

    找出匹配子串并返回迭代器,若无匹配返回空列表

  re.sub(pattern, repl, string, count=0, flags=0)

    将string中能被pattern匹配的子串替换为repl

    当count不为0时表示替换count次

    repl可以是一个函数

  re.subn(pattern, repl, string, count=0, flags=0)

    同re.sub() 但re.subn()返回包含心字符串和替换词数的二元组

  re.escape(string)

    对string中非字母数字进行转义

  re.purge()

    清除re缓存

  exception:

    re.error(msg, pattern=None, pos=None)

  Regular Expression Object:

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

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

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

    regex.split(string, maxsplit=0)

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

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

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

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

    regex.flags

    regex.groups

    regex.groupindex

    regex.pattern

  Match Object:

    match.expand(template)

    match.group([group1,...])

    match.groups(default=None)

    match.groupdict(default=None)

    match.start([group])  返回匹配开始位置

    match.end([group])  返回匹配结束位置

    match.span([group])  返回包含匹配起始结束位置的二元组

    match.pos

    match.endpos

    match.lastindex

    match.lastgroup

    match.re

    match.string

 

转载于:https://www.cnblogs.com/book-book/p/5429184.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值