Python正则表达式

     在python中,如果使用正则表达式的话,需要导入re模块,re模块是一个内置模块,直接import就可以使用。

     匹配字符串的几个方法

import re
s='hahaheheheihei'
#match是从字符串第一个字母开始匹配字符串(以XXX开头)
print(re.match(r'ha',s).group())

# search是从字符串的整个内容里面找,如果找到了就返回第一个,找不到就返回None
print(re.search('he',s).group())

#findall会把所有匹配的结果返回一个list,如果找不到的话,就返回一个空的list
print(re.findall('ha',s))

#把he改成ha
print(re.sub('he','ha',s))
print(s.replace('he','ha'))

#以he切割
print(re.split('he',s))
print(s.split('he'))

 

常用正则表达式符号

1、数量词

import re
s='besttest is best'

*号前面的那一个字符,可以没有也可以有N个
就是*号前面的那一个字符,可以没有也可以有N个
例如:ab* 可以匹配a ab abbbb abbbbbbb

print(re.findall(r'bee*',s))

 '+' 匹配前一个字符1次或多次,只是+前面的一个字符

print(re.findall(r'st+',s))#可以匹配st stt sttttt

'?' 匹配前一个字符1次或0次,只是?前面的一个字符

print(re.findall(r'st?',s))#可以匹配s st

# '{m}' 匹配前一个字符m次

print(re.findall(r'st{2}',s))#可以匹配stt

# '{n,m}' 匹配前一个字符n到m次

print(re.findall(r'st{1,2}',s))#可以匹配st stt

 

2、一般字符串

 '.' 默认匹配除\n之外的任意一个字符

print(re.findall(r'b.','bbesttest is ba  bi b bo'))
# >>>['bb', 'ba', 'bi', 'b ', 'bo']
'\'   转译符,前面的* + ?这样的字符都有特殊含义了,如果你想就想找它的话,那就得转译了
意思就是说如果你想让特殊字符失去以前的含义,那么就得给它前面加上\
print(re.findall('\?','besttest is best????'))
# >>>['?', '?', '?', '?']
'|'     匹配|左或|右的字符
print(re.findall(r'best|is','besttest is bestis'))
# >>>['best', 'is', 'best']
'[]' 字符集合,某些字符的集合,匹配的时候是这个集合里面的任意一个就行
print(re.findall(r'be[stacj]','besttest is best bejson bea'))
# >>>['bes', 'bes', 'bej']
在[]里面如果用^的话代表取反,也就是不包括的这些字符串的
print(re.findall(r'be[^stac]','besttest is best bejson bea'))
# >>>['bej']

 

3、边界匹配

'^'匹配以什么字符开头,多行情况下匹配每一行的开头
print(re.findall(r'^b','besttest is good',re.M))
print(re.findall(r'^b','besttest is good\nbest',re.M))#多行模式
# >>> ['b']
# >>> ['b','b']
'$'匹配以什么字符结尾,多行情况下匹配每一行的结尾
print(re.findall(r'd$','besttest is good'))
print(re.findall(r'd$','besttest is good\nbest is good',re.M))#多行模式
# >>> ['d']
# >>>['d','d']
'\A' 仅以什么字符开头,和^不同的是它不能用多行模式
print(re.findall(r'\Ab','besttest is good\nbest is good'))
# >>> ['b']
'\Z' 仅以什么字符结尾,和$不同的是它不能用多行模式
print(re.findall(r'd\Z','besttest is good\nbest is good'))
# >>> ['d']

 

4、预定义字符集合

'\d'  匹配数字0-9
print(re.findall(r'\d+','sdf234sdf2312sdfs'))
# >>> ['234','2312']
 '\D'    匹配非数字
print(re.findall(r'\D+','sdf2342312sdfs'))
# >>>['sdf', 'sdfs']
'\w'    匹配[A-Za-z0-9],也就是所有的字母和数字
print(re.findall(r'\w+','sdf234%^2312sdfs&'))
# >>>['sdf234', '2312sdfs']
'\W' 匹配不是[A-Za-z0-9],也就是不是字母和数字
print(re.findall(r'\W','sdf234%^2312sdfs&'))
# >>>['%', '^', '&']
'\s' 匹配空白字符、\t、\n、\r,空格
print(re.findall('\s','axss\n\tsdf\t\r\t'))
# >>> ['\n', '\t', '\t', '\r', '\t']
'\S'匹配空白字符,不是\t、\n、\r,空格
print(re.findall('\S','axss\n\tsdf\t\r\t'))
# >>>['a', 'x', 's', 's', 's', 'd', 'f']

 

5、分组匹配

'(...)' 分组匹配,把某些规则写成在一个组里,这样就可以直接对这个进行一些匹配了,举个例子的话,如果要匹配ip地址的话
ip地址是类似这样的192.168.5.1,每一位都是1位或者3位的数字然后后面有个点正常写的话,得这么写
print(re.findall(r'\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}',"192.168.1.3"))
>>> ['192.168.1.3']
这样写的话,有点麻烦了,通过上面的我们可以发现规律,除了第一个后面的全都是'.\d{1,3}',写重复的代码就是低级的,这样的话就可以用分组了
就把'.\d{1,3}'当做一个整体,然后让他们出现3次就ok了,可以改成下面这样的
print(re.search(r'\d{1,3}(.\d{1,3}){3}',"192.168.1.3").group())#这个是用search方法的,结果和上面的一样的
print(re.findall(r'\d{1,3}(?:.\d{1,3}){3}',"192.168.1.3"))#'?:'启用“不捕捉模式”
print(re.findall(r'(?:\d{1,3}.){3}\d{1,3}',"192.168.1.3"))#'?:'启用“不捕捉模式”
>>> ['192.168.1.3']
用match方法和search方法都是正常的,findall方法这里有个坑,就是如果findall方法里面有分组的话,那结果就只是分组里面的内容,要启用捕捉模式

正则表达式匹配模式

   正则匹配模式是用在match、search、findall里面的第三个参数,还有其他的模式,但是一般也用不到,就这两种能用到,别的就不记了

re.I: #忽略大小写
re.M: #多行模式,改变'^'和'$'的行为
re.findall('pattern', 'string',re.I)

 

posted @ 2017-08-16 15:00  查无此人sdy  阅读(236)  评论(0)    收藏  举报