悠悠小仙仙 发表于 2017-6-29 14:36:02

python-正则表达式

使用正则表达式时,需要导入包,import re ,简单使用如下:
匹配字符串的几个方法match :从第一个单词开始匹配,若匹配成功,则返回一个对象;若没有匹配数据,则返回None<font color="#0000ff">import re
s = 'besttest is gobeod be'
# match方法接收3个参数,第一个参数是匹配规则,也就是你要匹配的内容,第二个参数是要查找的字符串,第三个参数为非必填项
#match匹配的模式:从第一个单词开始匹配,如果匹配到了则返回一个对象,后面满足规则的匹配不到   输出>>>be
print(re.match(r'be', s).group())
s1 = 'besttest te is bat'
#match 若从第一个单词开始匹配,没有匹配到,则返回None      输出>>>None
print(re.match(r'te', s1))</font>
search: 从整个查找的字符串中进行匹配,若有多个数据满足匹配规则,则取第一个数据<pre><font color="#000000">s = 'besttest is gobeod '
# seach方法接收3个参数,第一个参数是匹配规则,也就是你要匹配的内容,第二个参数是要查找的字符串,第三个参数为非必填项
# seach匹配的模式:从字符串的整个内容中查找进行匹配,若匹配成功则返回一个对象;若未匹配成功,则返回None,输出>>st
print(re.search(r'st', s).group())         >>>输出st
s1 = 'besttest te is bat'
#seach 从字符串的整个内容中查找进行匹配,若有多个数据满足匹配规则,则返回第一个数据,输出>>te
print(re.search(r'te', s1).group())          >>>输出te
print(re.search(r'aa', s1))                  >>>输出None</font></pre>

findall: 从整个查找的字符串中进行匹配,若有多个数据满足匹配规则,则全部取出,返回list<font color="#0000ff">import re
s = 'besttest is gobeod be'
# findall方法接收3个参数,第一个参数是匹配规则,也就是你要匹配的内容,第二个参数是要查找的字符串,第三个参数为非必填项
# findall匹配的模式:从字符串的整个内容中查找进行匹配,若匹配成功则返回一个list
print(re.findall(r'be', s))               >>>输出['be', 'be', 'be']
s1 = 'besttest te is bat'
#findall从字符串的整个内容中查找进行匹配,若没有匹配到数据,则返回空list[]
print(re.findall(r'aa', s1))               >>>输出[]</font>
sub:从查找的字符串中进行匹配,若匹配成功,则进行替换,返回一个新值<pre><font color="#0000ff">import re
s = 'besttest is gobeod be'
#从查找的字符串中找到匹配的值,然后进行替换,返回一个新值,不改变原来的值;若匹配不到,则返回以前的值
new_s = re.sub(r'best', 'BEST', s)
print(new_s)                     >>>输出BESTtest is gobeod be
print(s)                         >>>输出besttest is gobeod be</font></pre>

spilt:从查找的字符串中进行匹配,若匹配成功,则以匹配的字符串进行分割,返回结果为list<font color="#0000ff">import re
s = 'besttest is gobeod beis '
#从查找的字符串中进行匹配,若匹配成功,则以is进行分割,返回结果为list
print(re.split(r'is', s))   >>>输出['besttest ', ' gobeod be', ' ']
#若从查找的字符串中没有找到匹配的字符,则将整个字符串作为元素,返回list
print(re.split(r'cc', s))   >>>输出['besttest is gobeod beis ']</font>
匹配符* :*号匹配前面的字符0次或多次,只匹配*前面的一个字符,即e出现的次数可以为0次或者多次,输出结果为list<font color="#0000ff">import re
s = 'besttest is gobeod bbef bede '
# 单独的 e字符是匹配失败的,*号匹配e的出现次数,但b字符必须存在
print(re.findall(r'be*', s))   >>>输出['be', 'be', 'b', 'be', 'be']</font>
匹配符+ :+号匹配前面的字符1次或多次,只匹配+前面的一个字符,即t出现的次数最少为1次,输出结果为list<font color="#0000ff">import re
s = 'besttest is gobeod sbest strt'
# 单独的t字符匹配是失败的,单独的s字符匹配也是失败的,t出现的次数最少为1次
print(re.findall(r'st+', s))      >>>输出['stt', 'st', 'st', 'st']</font>
匹配符? :?号匹配前面的字符0次或1次,只匹配?前面的一个字符,即t出现的次数可以为0次也可以为1次,输出结果为list<font color="#0000ff">import re
s = 'besttest is gobeod sbest strt'
# 匹配符?,匹配前一个字符出现的次数为0次或者1次,单独的t字符匹配失败,单独的s字符可以匹配成功;若都没有匹配的数据,则返回空list
print(re.findall(r'st?', s))         >>>>输出['st', 'st', 's', 's', 'st', 'st']</font>
匹配符{n} :{}号匹配字符串出现的次数,n代表出现的次数<font color="#0000ff">import re
s = 'besttest is gobeod sletter betterbesttt '
# 匹配 t出现2次的字符,单独的tt字符匹配失败
print(re.findall(r't{2}e', s))            >>>输出['tte', 'tte', 'tte']</font>
匹配符{n, m}:{}号匹配字符串出的n-m次<font color="#0000ff">import re
s = 'besttest is gobeod sletter betterttttetettttte t '
# 匹配 t出现1-4次的字符,可以出现1次,也可以最多出现4次,单独的t字符匹配失败
print(re.findall(r't{1,4}e', s))         >>>输出['tte', 'tte', 'tte', 'tttte', 'te', 'tttte']</font>
匹配符. 匹配任意字符<font color="#0000ff">import re
s = 'besttest is gobcod sletter bftterttttetettttte t '
#默认匹配\n之外的任意字符
print(re.findall(r'b.', s))   >>>输出['be', 'bc', 'bf']</font>
匹配符[]:匹配中括号内的任意字符即可<font color="#0000ff">import re
s = 'besttest is bej best bec bed bejg '
#匹配字符集合,匹配中括号内的任意一个字符即可,返回结果为list
print(re.findall(r'be', s))   >>>输出['bes', 'bej', 'bes', 'bec', 'bej']</font>
匹配符[^]:[^abcd]匹配不满足中括号内的任意字符,^取反符<font color="#0000ff">import re
s = 'besttest is bej best bec bed bejg '
#匹配符[^]取反,也就是匹配不到中括号内的任意字符
print(re.findall(r'be[^stcj]', s))   >>>输出['bed']</font>
匹配数字\d,匹配0-9数字<font color="#0000ff">import re
s = '123asd111233'
#匹配数字0-9,返回结果list
print(re.findall(r'\d', s))   >>>输出['1', '2', '3', '1', '1', '1', '2', '3', '3']</font>
匹配非数字\D,主要匹配大小写字母、中文、特殊字符<font color="#0000ff">import re
s = '123asdA@#&你好111233'
#匹配非数字,匹配大小写字母、中文、特殊字符,返回结果list
print(re.findall(r'\D', s))    >>>>输出['a', 's', 'd', 'A', '@', '#', '&', '你', '好']</font>
匹配符\w,匹配【a-zA-Z0-9】,匹配所有的字母和数字<div class="cnblogs_code"><pre><span style="color: #0000ff">import</span><span style="color: #000000"> re
s </span>= <span style="color: #800000">'</span><span style="color: #800000">123asdAcf@#&111233</span><span style="color: #800000">'</span>
<span style="color: #008000">#</span><span style="color: #008000">匹配,匹配所有的字母和数字,返回list</span>
<span style="color: #0000ff">print</span>(re.findall(r<span style="color: #800000">'</span><span style="color: #800000">\w</span><span style="color: #800000">'</span>, s))    >>>输出[<span style="color: #800000">'</span><span style="color: #800000">1</span><span style="color: #800000">'</span>, <span style="color: #800000">'</span><span style="color: #800000">2</span><span style="color: #800000">'</span>, <span style="color: #800000">'</span><span style="color: #800000">3</span><span style="color: #800000">'</span>, <span style="color: #800000">'</span><span style="color: #800000">a</span><span style="color: #800000">'</span>, <span style="color: #800000">'</span><span style="color: #800000">s</span><span style="color: #800000">'</span>, <span style="color: #800000">'</span><span style="color: #800000">d</span><span style="color: #800000">'</span>, <span style="color: #800000">'</span><span style="color: #800000">A</span><span style="color: #800000">'</span>, <span style="color: #800000">'</span><span style="color: #800000">c</span><span style="color: #800000">'</span>, <span style="color: #800000">'</span><span style="color: #800000">f</span><span style="color: #800000">'</span>, <span style="color: #800000">'</span><span style="color: #800000">1</span><span style="color: #800000">'</span>, <span style="color: #800000">'</span><span style="color: #800000">1</span><span style="color: #800000">'</span>, <span style="color: #800000">'</span><span style="color: #800000">1</span><span style="color: #800000">'</span>, <span style="color: #800000">'</span><span style="color: #800000">2</span><span style="color: #800000">'</span>, <span style="color: #800000">'</span><span style="color: #800000">3</span><span style="color: #800000">'</span>, <span style="color: #800000">'</span><span style="color: #800000">3</span><span style="color: #800000">']</span></pre></div><h4>
</h4>匹配符\W,匹配非【a-zA-Z0-9】,也就是匹配非字母和数字,匹配特殊字符和空格
<font color="#0000ff">import re
s = '123asdAcf@ # &你好111233'
#匹配,匹配所有的字母和数字,返回list
print(re.findall(r'\W', s))   >>>输出['@', ' ', '#', ' ', '&']</font>
匹配符\s,匹配空白符\t\n\r空格等<font color="#0000ff">import re
s = 'axss\n\tsdf\t\r\t'
#匹配\t\n\t空格
print(re.findall(r'\s', s))      >>>输出['\n', '\t', '\t', '\r', '\t']</font>
匹配符\S,匹配非空白字符,非\t\n\r空格等<font color="#0000ff">import re
s = 'axss\n\tsdf\t\r\t'
#匹配非\t\n\t空格
print(re.findall(r'\S', s))       >>>输出['a', 'x', 's', 's', 's', 'd', 'f']</font>
匹配手机号(包含13|15|18系列)<font color="#0000ff">import re
#匹配手机号
phone = '13462245029,12345678901,134000000002,1234'
#手机号以1开头,匹配第二位358中任意一位即可,后面9位取{9}取9位,如果手机号有长度为12位的,则取前11位
print(re.findall(r'1\d{9}', phone))</font>
匹配邮箱,email地址<font color="#0000ff">import re
mail = '123@163.com,@qq.com,123@.com,abd#163.com,abcd@163.com.cn'
#匹配邮箱必须有@符,@符前后得有必须出现一次,后缀可以是.com|.cn|.com.cn
print(re.findall(r'(\w+@\w+(\.com\.cn|\.com|\.cn))', mail))   >>>输出[('123@163.com', '.com'), ('abcd@163.com.cn', '.com.cn')]</font>
匹配url<font color="#0000ff">import re
url = 'http://www.baidu.com,https://autoho.cn,htpp://sougou.com.cn'
print(re.findall(r'http:\/\/[^\s]*', url))    >>>输出['http://www.baidu.com,https://autoho.cn,htpp://sougou.com.cn']</font>


巴黎的灯光下 发表于 2017-6-29 14:50:25

写的很详细,感谢分享!

悠悠小仙仙 发表于 2017-6-29 14:51:04

巴黎的灯光下 发表于 2017-6-29 14:50
写的很详细,感谢分享!

有帮助就好
页: [1]
查看完整版本: python-正则表达式