re 模块使 Python 语言拥有全部的正则表达式功能。
Python中常用的正则表达式处理函数如下:
1.re.match 函数
re.match 尝试从字符串的起始位置匹配一个模式,如果不是起始位置匹配成功的话,match()就返回none。
函数语法:
re.match(pattern,string,flags=0)
函数参数说明:
匹配成功re.match方法返回一个匹配的对象,否则返回None,若要完全匹配,pattern要以$结尾。
我们可以使用group(num) 或 groups() 匹配对象函数来获取匹配表达式。
实例如下:
-
#!/usr/bin/env python
-
# -*- coding:utf-8 -*-
-
# Author :Alvin.xie
-
# @Time :2017-11-14 15:02
-
# @file :zhenze.py
-
-
import re
-
-
s = '23432werwre2342werwrew'
-
p = r'(\d*)([a-zA-Z]*)'
-
m = re.match(p,s)
-
print m.group()
-
print m.group(0)
-
print m.group(1)
-
print m.group(2)
-
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() 匹配对象函数来获取匹配表达式。
-
#!/usr/bin/env python
-
# -*- coding:utf-8 -*-
-
# Author :Alvin.xie
-
# @Time :2017-11-14 15:02
-
# @file :zhenze.py
-
-
import re
-
text = "JGood is a handsome boy, he is cool, clever, and so on..."
-
m = re.search(r'\shan(ds)ome\s', text)
-
if m:
-
print m.group(0), m.group(1)
-
else:
-
print 'not search'
执行结果:
handsome ds
re.match与re.search的区别:re.match只匹配字符串的开始,如果字符串开始不符合正则表达式,则匹配失败,函数返回None;而re.search匹配整个字符串,直到找到一个匹配。
re.split
可以使用re.split来分割字符串,如:re.split(r'\s+', text);将字符串按空格分割成一个单词列表。
-
#!/usr/bin/env python
-
# -*- coding:utf-8 -*-
-
# Author :Alvin.xie
-
# @Time :2017-11-14 15:02
-
# @file :zhenze.py
-
-
import re
-
m = re.split("[0-9]", "I1Love2Python3hehe centos8")
-
print m
执行结果:
['I', 'Love', 'Python', 'hehe centos', '']
re.findall
re.findall可以获取字符串中所有匹配的字符串。如:re.findall(r'\w*oo\w*', text);获取字符串中,包含'oo'的所有单词。
-
#!/usr/bin/env python
-
# -*- coding:utf-8 -*-
-
# Author :Alvin.xie
-
# @Time :2017-11-14 15:02
-
# @file :zhenze.py
-
-
import re
-
n = re.findall("[0-9]", "I1Love2Python3hehe centos8")
-
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创建的模式对象。如下:
-
#!/usr/bin/env python
-
# -*- coding:utf-8 -*-
-
# Author :Alvin.xie
-
# @Time :2017-11-14 15:02
-
# @file :zhenze.py
-
-
import re
-
some_text = 'a,b,,,,c d'
-
m = re.compile('[, ]+')
-
print m.split(some_text)
执行结果:
['a', 'b', 'c', 'd']
如果不使用re.compile
在进行search,match等操作前不适用compile函数,会导致重复使用模式时,需要对模式进行重复的转换。降低匹配速度。而此种方法的调用方式,更为直观。如下:
-
#!/usr/bin/env python
-
# -*- coding:utf-8 -*-
-
# Author :Alvin.xie
-
# @Time :2017-11-14 15:02
-
# @file :zhenze.py
-
-
import re
-
some_text = 'a,b,,,,c d'
-
print re.split('[, ]+',some_text)
执行结果:
['a', 'b', 'c', 'd']
阅读(1634) | 评论(0) | 转发(0) |