python中的正则表达式
- 正则表达式用途
正则表达式是用来(只用来)匹配字符串的;
现实操作中python的字符串内置处理函数均为绝对处理,完全匹配的操作:
s = 'hello world'
#传统的字符串内置函数均为绝对匹配
print(s.find('o'))
ret = s.replace(' ','-')
print(ret)
'''
'''
但经常在应用中需要使用到模糊匹配的情况:
如:需要用户输入电话号码:
138********
对于用户输入的一串伪电话号码的字符串,需要判断:不包含字母、区号合法(移动138、电信189等等规则)、位数正确等等;(有人说这要求使用内置函数好像可以做出了,那么继续)
如:需要留网址,邮件等
http://www.*******.com/cn
******@*****.com/.cn
此时python字符串的内置函数就完全无法对其进行模糊处理,于是python中就需要一种更加强大的字符串匹配方式----正则表达式应运而生。
-
正则表达式的方法
1、findall()
:将所有结果返回到一个列表中import re ret = re.findall('ll','hello world') print(ret) #['ll']
2、
search()
:返回符合匹配的第一个
对象,对象使用group()
方式返回结果import re ret = re.search('ll','hello world') print(ret) # <_sre.SRE_Match object; span=(2, 4), match='ll'> print(ret.group()) # ll
3、
match
:只在字符串开始匹配,符合即返回匹配到的对象import re ret = re.match('h','hello,honey') print(ret) # <_sre.SRE_Match object; span=(0, 1), match='h'> print(ret.group()) # h
4、
split
:按要求分割字符串import re ret = re.split('o','hello,honey') print(ret) # ['hell', ',h', 'ney']
5、
sub
:按要求替换字符串中指定内容import re ret = re.sub('o','O','hello,honey') print(ret) # hellO,hOney
6、
compile
:将所需要的匹配规则编译成一个规则对象,供其他字符串处理方法使用import re obj = re.compile('com') ret = obj.findall('www.4399.com') print(ret) # ['com']
-
正则表达式的元字符(特殊功能含义的符号)
1、.
:通配符 代值一个任意字符(不包括换行符)import re # . 通配符 代指一个任意字符(不包括换号符) ret = re.findall('w..l','hello world') # 找到‘w开头中间随意两个字符,l结尾’ print(ret) # ['worl'] ret = re.findall('w.l','hello w\nld') # 换行符不包含在内 print(ret) # []
2、
^
:尖角符 由字符起始处开始匹配import re # ^ 尖角符 匹配开始 ret = re.findall('^h...o','hello world') # 匹配以h加任意3个字符加o开始 print(ret) # ['hello'] ret = re.match('h...o','hello world') # 从开始匹配,<_sre.SRE_Match object; span=(0, 5), match='hello'> print(ret.group()) # hello
3、
$
:Dollar符 匹配字符串结尾处内容import re # $ dollar符 匹配结尾 ret = re.findall('w...d$','hallo world') # 匹配以w加3个任意字符加d结尾 print(ret) # ['world']
4、
*
:重复匹配符 重复匹配前一个字符,数量为0到正无穷,包含0个import re # * 重复匹配符 重复匹配前面一个字符(0到∞,包括0个) ret = re.findall('a.*l','fjsdjjsljaadedlfdsf') # 匹配以a开始,多个. 即多个任意字符,l结束 print(ret) # [