python-正则表达式

使用正则表达式时,需要导入包,import re ,简单使用如下:

匹配字符串的几个方法

match :从第一个单词开始匹配,若匹配成功,则返回一个对象;若没有匹配数据,则返回None

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))

search: 从整个查找的字符串中进行匹配,若有多个数据满足匹配规则,则取第一个数据

import re
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

findall: 从整个查找的字符串中进行匹配,若有多个数据满足匹配规则,则全部取出,返回list

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))               >>>输出[]

sub:从查找的字符串中进行匹配,若匹配成功,则进行替换,返回一个新值

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

spilt:从查找的字符串中进行匹配,若匹配成功,则以匹配的字符串进行分割,返回结果为list

import re
s = 'besttest is gobeod be  is '
#从查找的字符串中进行匹配,若匹配成功,则以is进行分割,返回结果为list
print(re.split(r'is', s))     >>>输出['besttest ', ' gobeod be  ', ' ']
#若从查找的字符串中没有找到匹配的字符,则将整个字符串作为元素,返回list
print(re.split(r'cc', s))     >>>输出['besttest is gobeod be  is ']

匹配符* :*号匹配前面的字符0次或多次,只匹配*前面的一个字符,即e出现的次数可以为0次或者多次,输出结果为list

import re
s = 'besttest is gobeod b  bef bed  e '
# 单独的 e字符是匹配失败的,*号匹配e的出现次数,但b字符必须存在
print(re.findall(r'be*', s))     >>>输出['be', 'be', 'b', 'be', 'be']

匹配符+ :+号匹配前面的字符1次或多次,只匹配+前面的一个字符,即t出现的次数最少为1次,输出结果为list

import re
s = 'besttest is gobeod s  best str  t  '
# 单独的t字符匹配是失败的,单独的s字符匹配也是失败的,t出现的次数最少为1次
print(re.findall(r'st+', s))        >>>输出['stt', 'st', 'st', 'st']

匹配符? :?号匹配前面的字符0次或1次,只匹配?前面的一个字符,即t出现的次数可以为0次也可以为1次,输出结果为list

import re
s = 'besttest is gobeod s  best str  t  '
# 匹配符?,匹配前一个字符出现的次数为0次或者1次,单独的t字符匹配失败,单独的s字符可以匹配成功;若都没有匹配的数据,则返回空list
print(re.findall(r'st?', s))         >>>>输出['st', 'st', 's', 's', 'st', 'st']

匹配符{n} :{}号匹配字符串出现的次数,n代表出现的次数

import re
s = 'besttest is gobeod s  letter better  best  tt '
# 匹配 t出现2次的字符,单独的tt字符匹配失败
print(re.findall(r't{2}e', s))            >>>输出['tte', 'tte', 'tte']

匹配符{n, m}:{}号匹配字符串出的n-m次

import re
s = 'besttest is gobeod s  letter better  tttte  te  ttttte t '
# 匹配 t出现1-4次的字符,可以出现1次,也可以最多出现4次,单独的t字符匹配失败
print(re.findall(r't{1,4}e', s))         >>>输出['tte', 'tte', 'tte', 'tttte', 'te', 'tttte']

匹配符. 匹配任意字符

import re
s = 'besttest is gobcod s  letter bftter  tttte  te  ttttte t '
#默认匹配
之外的任意字符
print(re.findall(r'b.', s))   >>>输出['be', 'bc', 'bf']

匹配符[]:[abcd]匹配中括号内的任意字符即可

import re
s = 'besttest is bej best bec bed bejg '
#匹配字符集合,匹配中括号内的任意一个字符即可,返回结果为list
print(re.findall(r'be[stcj]', s))     >>>输出['bes', 'bej', 'bes', 'bec', 'bej']

匹配符[^]:[^abcd]匹配不满足中括号内的任意字符,^取反符

import re
s = 'besttest is bej best bec bed bejg '
#匹配符[^]取反,也就是匹配不到中括号内的任意字符
print(re.findall(r'be[^stcj]', s))   >>>输出['bed']

 匹配数字d,匹配0-9数字

import re
s = '123asd111233'
#匹配数字0-9,返回结果list
print(re.findall(r'd', s))   >>>输出['1', '2', '3', '1', '1', '1', '2', '3', '3']

匹配非数字D,主要匹配大小写字母、中文、特殊字符

import re
s = '123asdA@#&你好111233'
#匹配非数字,匹配大小写字母、中文、特殊字符,返回结果list
print(re.findall(r'D', s))    >>>>输出['a', 's', 'd', 'A', '@', '#', '&', '', '']

匹配符w,匹配【a-zA-Z0-9】,匹配所有的字母和数字

import re
s = '123asdAcf@#&111233'
#匹配[a-zA-Z0-9],匹配所有的字母和数字,返回list
print(re.findall(r'w', s))    >>>输出['1', '2', '3', 'a', 's', 'd', 'A', 'c', 'f', '1', '1', '1', '2', '3', '3']

匹配符W,匹配非【a-zA-Z0-9】,也就是匹配非字母和数字,匹配特殊字符和空格

import re
s = '123asdAcf@ # &你好111233'
#匹配[a-zA-Z0-9],匹配所有的字母和数字,返回list
print(re.findall(r'W', s))   >>>输出['@', ' ', '#', ' ', '&']

匹配符s,匹配空白符 空格等

import re
s = 'axss
	sdf	
	'
#匹配	
	空格
print(re.findall(r's', s))      >>>输出['
', '	', '	', '
', '	']

匹配符S,匹配非空白字符,非 空格等

import re
s = 'axss
	sdf	
	'
#匹配非	
	空格
print(re.findall(r'S', s))       >>>输出['a', 'x', 's', 's', 's', 'd', 'f']

匹配手机号(包含13|15|18系列)

import re
#匹配手机号
phone = '13462245029,12345678901,134000000002,1234'
#手机号以1开头,[358]匹配第二位358中任意一位即可,后面9位取[0-9]{9}取9位,如果手机号有长度为12位的,则取前11位
print(re.findall(r'1[358]d{9}', phone))

 匹配邮箱,email地址

import re
mail = '123@163.com,@qq.com,123@.com,abd#163.com,abcd@163.com.cn'
#匹配邮箱必须有@符,@符前后得有[a-zA-Z0-9]必须出现一次,后缀可以是.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')]

匹配url

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']
原文地址:https://www.cnblogs.com/lhly/p/7067158.html