码迷,mamicode.com
首页 > 编程语言 > 详细

re模块 | Python 3.5.1

时间:2016-04-25 06:40:13      阅读:411      评论:0      收藏:0      [点我收藏+]

标签:

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

 

re模块 | Python 3.5.1

标签:

原文地址:http://www.cnblogs.com/book-book/p/5429184.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!