Chinaunix首页 | 论坛 | 博客
  • 博客访问: 229772
  • 博文数量: 57
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 557
  • 用 户 组: 普通用户
  • 注册时间: 2015-10-01 18:05
文章分类

全部博文(57)

文章存档

2017年(57)

我的朋友

分类: Python/Ruby

2017-11-15 11:59:01

re 模块使 Python 语言拥有全部的正则表达式功能。
Python中常用的正则表达式处理函数如下:
1.re.match 函数
re.match 尝试从字符串的起始位置匹配一个模式,如果不是起始位置匹配成功的话,match()就返回none。
函数语法:
re.match(pattern,string,flags=0)
函数参数说明:

匹配成功re.match方法返回一个匹配的对象,否则返回None,若要完全匹配,pattern要以$结尾。

我们可以使用group(num) 或 groups() 匹配对象函数来获取匹配表达式。

实例如下:

点击(此处)折叠或打开

  1. #!/usr/bin/env python
  2. # -*- coding:utf-8 -*-
  3. # Author :Alvin.xie
  4. # @Time :2017-11-14 15:02
  5. # @file :zhenze.py

  6. import re

  7. s = '23432werwre2342werwrew'
  8. p = r'(\d*)([a-zA-Z]*)'
  9. m = re.match(p,s)
  10. print m.group()
  11. print m.group(0)
  12. print m.group(1)
  13. print m.group(2)
  14. print m.groups()
执行结果如下:
23432werwre
23432werwre
23432
werwre
('23432', 'werwre')
2.re.search函数

re.search 扫描整个字符串并返回第一个成功的匹配。

函数语法:
re.search(pattern,string,flags=0)

匹配成功re.search方法返回一个匹配的对象,否则返回None。如果string中存在多个pattern子串,只返回第一个。

我们可以使用group(num) 或 groups() 匹配对象函数来获取匹配表达式。

点击(此处)折叠或打开

  1. #!/usr/bin/env python
  2. # -*- coding:utf-8 -*-
  3. # Author :Alvin.xie
  4. # @Time :2017-11-14 15:02
  5. # @file :zhenze.py

  6. import re
  7. text = "JGood is a handsome boy, he is cool, clever, and so on..."
  8. m = re.search(r'\shan(ds)ome\s', text)
  9. if m:
  10.     print m.group(0), m.group(1)
  11. else:
  12.     print 'not search'
执行结果:
handsome  ds
re.match与re.search的区别:re.match只匹配字符串的开始,如果字符串开始不符合正则表达式,则匹配失败,函数返回None;而re.search匹配整个字符串,直到找到一个匹配。

re.split

  可以使用re.split来分割字符串,如:re.split(r'\s+', text);将字符串按空格分割成一个单词列表。

点击(此处)折叠或打开

  1. #!/usr/bin/env python
  2. # -*- coding:utf-8 -*-
  3. # Author :Alvin.xie
  4. # @Time :2017-11-14 15:02
  5. # @file :zhenze.py

  6. import re
  7. m = re.split("[0-9]", "I1Love2Python3hehe centos8")
  8. print m
执行结果:
['I', 'Love', 'Python', 'hehe centos', '']

re.findall

  re.findall可以获取字符串中所有匹配的字符串。如:re.findall(r'\w*oo\w*', text);获取字符串中,包含'oo'的所有单词。

点击(此处)折叠或打开

  1. #!/usr/bin/env python
  2. # -*- coding:utf-8 -*-
  3. # Author :Alvin.xie
  4. # @Time :2017-11-14 15:02
  5. # @file :zhenze.py

  6. import re
  7. n = re.findall("[0-9]", "I1Love2Python3hehe centos8")
  8. print n
执行结果:
['1', '2', '3', '8']

re.compile

  re模块中包含一个重要函数是compile(pattern [, flags]) ,该函数根据包含的正则表达式的字符串创建模式对象。可以实现更有效率的匹配。在直接使用字符串表示的正则表达式进行search,match和findall操作时,python会将字符串转换为正则表达式对象。而使用compile完成一次转换之后,在每次使用模式的时候就不用重复转换。当然,使用re.compile()函数进行转换后,re.search(pattern, string)的调用方式就转换为 pattern.search(string)的调用方式。

其中,后一种调用方式中,pattern是用compile创建的模式对象。如下:

点击(此处)折叠或打开

  1. #!/usr/bin/env python
  2. # -*- coding:utf-8 -*-
  3. # Author :Alvin.xie
  4. # @Time :2017-11-14 15:02
  5. # @file :zhenze.py

  6. import re
  7. some_text = 'a,b,,,,c d'
  8. m = re.compile('[, ]+')
  9. print m.split(some_text)

执行结果:
['a', 'b', 'c', 'd']
如果不使用re.compile

在进行search,match等操作前不适用compile函数,会导致重复使用模式时,需要对模式进行重复的转换。降低匹配速度。而此种方法的调用方式,更为直观。如下:

点击(此处)折叠或打开

  1. #!/usr/bin/env python
  2. # -*- coding:utf-8 -*-
  3. # Author :Alvin.xie
  4. # @Time :2017-11-14 15:02
  5. # @file :zhenze.py

  6. import re
  7. some_text = 'a,b,,,,c d'
  8. print re.split('[, ]+',some_text)
执行结果:
['a', 'b', 'c', 'd']




阅读(1499) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~