在python中加载正则表达式模块re:
import re
1. python的正则表达式
函数 |
说明 |
案例 |
match |
从字符串的起始部分对模式进行匹配。成功就返回对象,失败返回None |
re.match(‘foo’,’foo’) |
search |
在字符串的任意位置对给定正则表达式模式搜索第一次出现的匹配情况 |
re.search(‘foo’,’seafood’) |
findall |
查询字符串中某个正则表达式模式全部的非重复出现情况。成功返回包含所有结果的列表,失败返回空列表 |
re.findall(‘car’ ,’tcar carry a car’) |
group |
匹配对象的方法,返回整个匹配对象或者特定子组 |
|
compile |
编译正则表达式 |
|
sub |
替换表达式中的正则表达式(将testStr中的'foo'替换成‘FOO',并返回一个新的字符串) |
re.sub(‘foo’,’FOO’,’testStr’) |
2. “ | ” 在多个模式中选其一
python里面首先需要将需匹配的模式编译成正则支持的模式compiled pattern,re.compile(),下例中匹配的字符串为"邮箱"或"电话"。
a1 = re.compile("邮箱|电话")
# 管道符号“|”,表示从多个模式中选择其一,re.findall(a,b),a为需要寻找的字符(a必须为string或compiled pattern),b为待寻找的文本,凡是b中包含a里面指定的字符,都要找出来。
下例中在指定文本test1中寻找字符串“邮箱”或“电话”:
test1 = "我的邮箱为[email protected],我的电话号码为123456789"
re.findall(a1,test1)
[out]: ['邮箱','电话]
3. “ . ” 匹配任意一个字符,几个"."就匹配几个字符
a2 = re.compile("我的..")
re.findall(a2, test1)
[out]:["我的邮箱","我的电话"]
若要查找的字符里面包含“ . ”,则使用转义字符a2 = re.compile("@163\.com")
4. 方括号" [ ] ",匹配方括号里面的任意一个字符 ( "a[bc]d", 匹配abd或acd )
a3 = re.compile("我的[邮箱电话]")
re.findall(a3, test1)
[out]:['我的邮','我的电']
方括号里还可以限定范围[A-Z]和否定[^]
[A-Z0-9a-z] 匹配任意一个A到Z,0到9,a到z的字符
[^0-9] 匹配任意非0到9的字符
5. 匹配不止一次:
* 匹配其左边的正则表达式出现零次或者多次
+ 匹配其左边的正则表达式出现一次或者多次
?匹配其左边的正则表达式出现一次或者零次
大括号{ } 里面是单个值或者是一对逗号分隔的值
test2 = "121315211"
a4 = re.compile("1*")
re.findall(a4, test2)
[out]: ['1', '', '1', '', '1', '', '', '11', '']
# * 匹配*左边的字符0次或多次
a5 = re.compile("1+")
re.findall(a5, test2)
[out]: ['1', '1', '1', '11']
# + 匹配+左边的字符1次或者多次
a6 = re.compile("1?")
re.findall(a6, test2)
[out]: ['1', '', '1', '', '1', '', '', '1', '1', '']
# ? 匹配?左边的字符0次或者1次
a7 = re.compile("[12]+")
re.findall(a7, test2)
[out]: ['121', '1', '211']
# []+ 匹配方括号里面的任意组合1次或多次
a8 = re.compile("[12]")
re.findall(a8, test2)
[out]: ['1', '2', '1', '1', '2', '1', '1']
# 若不想组合匹配,只想单个匹配,直接用方括号就好
a9 = re.compile("[12]{2,3}")
re.findall(a9, test2)
[out]: ['121', '211']
# 使用大括号{}限定匹配几个字符,{2,3}限定匹配2至3个字符
6. 表示字符集的特殊字符
\d 匹配任何十进制数字
\w 匹配任何字母和数字的字符集
\s 表示空格字符
大写\D、\W、\S表示反面,不匹配...
test2 = "小红家住在北京路1035号12弄177栋,小绿家住在南京路25号8弄28栋,小黄家住在福州路33号21弄栋"
b1 = re.compile("\d+号\d+弄\d+栋")
re.findall(b1,test2)
[out]: ['1035号12弄177栋', '25号8弄28栋']
# \d匹配任意十进制数字,+匹配左边字符1次或多次,但小黄家的楼栋为缺失值,没有被匹配到,得用*匹配0次或多次
b2 = re.compile("\d+号\d+弄\d*栋")
re.findall(b2,test2)
[out]: ['1035号12弄177栋', '25号8弄28栋', '33号21弄栋']
匹配邮箱:
test3 = "小红的邮箱是[email protected],小蓝的邮箱是[email protected],小绿的邮箱是[email protected]"
c1 = re.compile("\w+@\w+\.\w+")
re.findall(c1, test3)
[out]: ['小红的邮箱是[email protected]',
'小蓝的邮箱是[email protected]',
'小绿的邮箱是[email protected]']
# \w+ 匹配任何字母和数字的集合,但在此处也匹配了我们不需要的中文字符,使用方括号"[a-zA-Z0-9]+"除掉中文
c2 = re.compile("[a-zA-Z0-9]+@\w+\.\w+")
re.findall(c2, test3)
[out]: ['[email protected]', '[email protected]', '[email protected]']
# 踢掉中文后,因为邮箱中包含有下划线“_”,没有被选中,应在方括号中加下划线
c3 = re.compile("[a-zA-Z0-9_]+@\w+\.\w+")
re.findall(c3, test3)
[out]: ['[email protected]', '[email protected]', '[email protected]']
匹配中文:re.compile("[\u4e00-\u9fa5]")
[\u4e00-\u9fa5]为中文的编码区间
test3 = "小红的邮箱是[email protected],小蓝的邮箱是[email protected],小绿的邮箱是[email protected]"
d1 = re.compile("[\u4e00-\u9fa5]")
re.findall(d1,test3)
[out]: ['小',
'红',
'的',
'邮',
'箱',
'是',
'小',
'蓝',
'的',
'邮',
'箱',
'是',
'小',
'绿',
'的',
'邮',
'箱',
'是']
# 匹配任意一个中文字符
d2 = re.compile("[\u4e00-\u9fa5]+")
re.findall(d2,test3)
[out]: ['小红的邮箱是', '小蓝的邮箱是', '小绿的邮箱是']
# 也可以用非“^”来匹配中文字符
d3 = re.compile("[^a-zA-z0-9_,@\.]")
re.findall(d3,test3)
[out]: ['小',
'红',
'的',
'邮',
'箱',
'是',
'小',
'蓝',
'的',
'邮',
'箱',
'是',
'小',
'绿',
'的',
'邮',
'箱',
'是']
中文识别:
•最好确保文本编码为utf-8
•中文模式:[\u4e00-\u9fa5]
•那么英文如何识别呢?
•[a-zA-Z]