re模块

一:re.compile(pattern, flags=0),返回re.match对象

   模块提供了 re.compile() 函数将一个字符串编译成 pattern object,用于匹配或搜索,re.compile() 还接受一个可选的参数 flag,用于指定正则匹配的模式

re.compile()就是要匹配的东西的对象

二:re.match(string, pos, endpos)

  匹配从 pos 到 endpos 的字符子串的开头。匹配成功返回一个 match object,不匹配返回 None。

  pos 的默认值是0,endpos 的默认值是 len(string),所以默认情况下是匹配整个字符串的开头。

二:match的更多用法

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

  返回 match object 中的字符串。
  每一个 ( ) 都是一个分组,分组编号从1开始
  组 0 总是存在的,它就是整个表达式 。
  没有参数时,group默认为0,这时返回整个匹配到的字符串。
  指定一个参数(整数)时,返回该分组匹配到的字符串。
  指定多个参数时,返回由那几个分组匹配到的字符串组成的 tuple

1 import re
2 pattern=re.compile('li')
3 print(pattern.match('lithis is flag'))#只匹配开头为li的
4 print(pattern.match('thisli do not link'))
5 print(pattern.match('this li my web',5))#指定开始位置匹配开头为li的
用法
 1 pattern=re.compile(r'(w+) (w+)')
 2 res=pattern.match("Keven Durant,Warrior")
 3 print(res)
 4 print(res.group())#默认下返回所有匹配的数据
 5 print(res.group(1))#返回匹配到的组1的数据
 6 print(res.group(1,2))#返回匹配到的组1,2的数据
 7 ---------------------------------------------结果
 8 <re.Match object; span=(0, 12), match='Keven Durant'>
 9 Keven Durant
10 Keven
11 ('Keven', 'Durant')
用法

match.groups()

  • 返回由所有分组匹配到的字符串组成的 tuple。

match.start([group])

  • 没有参数时,返回匹配到的字符串的起始位置。
  • 指定参数(整数)时,返回该分组匹配到的字符串的起始位置。
1 pattern=re.compile(r'(w+) (w+)')
2 res=pattern.match("Keven Durant,Warrior")
3 print(res.start())#返回匹配到的字符串的开始位置#0
4 print(res.start(2))#返回匹配到组的起始位置#6
用法

match.end([group])

  • 没有参数时,返回匹配到的字符串的结束位置。
  • 指定参数(整数)时,返回该分组匹配到的字符串的结束位置。
1 pattern=re.compile(r'(w+) (w+)')
2 res=pattern.match("Keven Durant,Warrior")
3 print(res.end())#12    
4 print(res.end(1))#5
用法

match.span([group])

  • 返回一个二元 tuple 表示匹配到的字符串的范围,即 (start, end)。
  • 指定参数时,返回该分组匹配到的字符串的 (start, end)。
1 pattern = re.compile(r"(w+) (w+)")
2 m = pattern.match("Kobe Bryant, Lakers")
3 print(m.span())     # (0, 11)
4 print(m.span(2))    # (5, 11)
用法

三:re.search(string, pos, endpos):返回re.match对象

  扫描整个字符串,并返回它找到的第一个匹配(Match object)。

  和 re.match() 一样,可以通过 pos 和 endpos 指定范围

1 pattern=re.compile('is')
2 print(pattern.search('this is my web,it is extemely bad'))
3 
4 <re.Match object; span=(2, 4), match='is'>
用法

四:re.findall(string, pos, endpos):返回列表

  找到所有匹配的子串,并返回一个 list 。

  可选参数 pos 和 endpos 同上。

1 pattern=re.compile('is')
2 print(pattern.findall('this is my web,it is so cool'))
用法

五:re.finditer(string, pos, endpos):返回迭代器,迭代后返回re.match对象

  找到所有匹配的子串,并返回由这些匹配结果(match object)组成的迭代器。

  可选参数 pos 和 endpos 和上面一样。

 1 pattern=re.compile('is')
 2 res=pattern.finditer('this is my girl,she is so shy')
 3 print(res)
 4 for item in res:
 5     print(item)
 6 结果
 7 <callable_iterator object at 0x000001E5D2E78288>
 8 <re.Match object; span=(2, 4), match='is'>
 9 <re.Match object; span=(5, 7), match='is'>
10 <re.Match object; span=(20, 22), match='is'>
用法

六:re.split(string, maxsplit=0)

  maxsplit用于指定最大分割次数,不指定将全部分割

1 pattern=re.compile(r'[A-Z]+')
2 m=pattern.split('ThisIMyweb,YOUcansurFaCeiT')
3 print(m)
4 ---------------------------------
5 pattern=re.compile(r'[A-Z]+')
6 m=pattern.split('ThisIMyweb,YOUcansurFaCeiT')
7 print(m)
用法

七:re.sub(repl, string, count=0)

  使用 repl 替换 string 中每一个匹配的子串,返回替换后的字符串。若找不到匹配,则返回原字符串。
  repl 可以是一个字符串,也可以是一个函数。
  当repl是一个字符串时,任何在其中的反斜杠都会被处理。
  当repl是一个函数时,这个函数应当只接受一个参数(Match对象),并返回一个字符串用于替换。
  count 用于指定最多替换次数,不指定时全部替换

 1 def up(string):
 2     return string.group().upper()
 3 pattern=re.compile(r'is')
 4 res=pattern.sub('are','this is my web',1)
 5 fes=pattern.sub(up,'this is my web')
 6 print(res)
 7 print(fes)
 8 --------------------------------------------------
 9 thare is my web
10 thIS IS my web
用法

八:re.subn(repl, string, count=0)

  同 sub(),只不过返回值是一个二元 tuple,即(sub函数返回值, 替换次数)

1.‘.’

  '.' 匹配任意字符(即通配符),若指定了re.S,则可以匹配换行符

2.'^'

  '^' 匹配行首,即字符串的开头,若指定了re.M,会自动匹配每行开头

3.'$'

  '$' 匹配行尾,若制定了re.M,会自动匹配每行的开头

4.'*'

  '*'匹配'*'前面一个字符大于等于0次

  如果规则里只有一个分组,尽量避免用*否则会有可能匹配出空字符串

5.‘+’

  '+' 匹配‘+’前一个字符大于等于1个

6.'?'

  匹配‘?’前一个字符0个或1个

  还有一个功能是可以防止贪婪匹配,详情见防贪婪匹配

7.'{}'

  {m}匹配前一个字符m个

  {m,n}匹配前一个字符m-n个

8.'[]'

  匹配[]中的任意个字符集,所有特殊字符在字符集中都失去其原有的特殊含义。用反斜杠转义恢复特殊字符的特殊含义。

9.‘'|'

  匹配前一个或后一个字符

10.('...')

  以组的方式匹配括号中的字符。每一个()即为一组

11.(?P<name>'...') 指定匹配的组的名称

1.'d'  匹配所有的十进制数即【0-9】

2.‘D’  匹配任何非数字字符即【^0-9】

3.'s'   匹配任何空白字符即[ fv]

4.'S'  匹配任何非空白字符即【^ fv】

5.'w'  匹配任何包括下划线在内任何字母数字字符,即【a-zA-Z0-9_】

1.re.S:使‘.’匹配包括换行在内的所有字符

2.re.I:是匹配对大小写不敏感

3.re.L:做本地化识别匹配

4.re.M:多行匹配,影响'^'和'$'

原文地址:https://www.cnblogs.com/Mr-l/p/11257908.html