python-正则表达式,,使用正则表达式时,需


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

匹配字符串的几个方法

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

import res = ‘besttest is gobeod be  ‘# match方法接收3个参数,第一个参数是匹配规则,也就是你要匹配的内容,第二个参数是要查找的字符串,第三个参数为非必填项#match匹配的模式:从第一个单词开始匹配,如果匹配到了则返回一个对象,后面满足规则的匹配不到     输出>>>beprint(re.match(r‘be‘, s).group())s1 = ‘besttest te is bat‘#match 若从第一个单词开始匹配,没有匹配到,则返回None        输出>>>Noneprint(re.match(r‘te‘, s1))

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

import res = ‘besttest is gobeod ‘# seach方法接收3个参数,第一个参数是匹配规则,也就是你要匹配的内容,第二个参数是要查找的字符串,第三个参数为非必填项# seach匹配的模式:从字符串的整个内容中查找进行匹配,若匹配成功则返回一个对象;若未匹配成功,则返回None,输出>>stprint(re.search(r‘st‘, s).group())         >>>输出sts1 = ‘besttest te is bat‘#seach 从字符串的整个内容中查找进行匹配,若有多个数据满足匹配规则,则返回第一个数据,输出>>teprint(re.search(r‘te‘, s1).group())          >>>输出teprint(re.search(r‘aa‘, s1))                  >>>输出None

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

import res = ‘besttest is gobeod be‘# findall方法接收3个参数,第一个参数是匹配规则,也就是你要匹配的内容,第二个参数是要查找的字符串,第三个参数为非必填项# findall匹配的模式:从字符串的整个内容中查找进行匹配,若匹配成功则返回一个listprint(re.findall(r‘be‘, s))                 >>>输出[‘be‘, ‘be‘, ‘be‘]s1 = ‘besttest te is bat‘#findall从字符串的整个内容中查找进行匹配,若没有匹配到数据,则返回空list[]print(re.findall(r‘aa‘, s1))               >>>输出[]

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

import res = ‘besttest is gobeod be‘#从查找的字符串中找到匹配的值,然后进行替换,返回一个新值,不改变原来的值;若匹配不到,则返回以前的值new_s = re.sub(r‘best‘, ‘BEST‘, s)print(new_s)                     >>>输出BESTtest is gobeod beprint(s)                         >>>输出besttest is gobeod be

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

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

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

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

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

import res = ‘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 res = ‘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 res = ‘besttest is gobcod s  letter bftter  tttte  te  ttttte t ‘#默认匹配\n之外的任意字符print(re.findall(r‘b.‘, s))   >>>输出[‘be‘, ‘bc‘, ‘bf‘]

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

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

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

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

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

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

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

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

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

import res = ‘123asdAcf@#&111233‘#匹配[a-zA-Z0-9],匹配所有的字母和数字,返回listprint(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 res = ‘123asdAcf@ # &你好111233‘#匹配[a-zA-Z0-9],匹配所有的字母和数字,返回listprint(re.findall(r‘\W‘, s))   >>>输出[‘@‘, ‘ ‘, ‘#‘, ‘ ‘, ‘&‘]

匹配符\s,匹配空白符\t\n\r空格等

import res = ‘axss\n\tsdf\t\r\t‘#匹配\t\n\t空格print(re.findall(r‘\s‘, s))      >>>输出[‘\n‘, ‘\t‘, ‘\t‘, ‘\r‘, ‘\t‘]

匹配符\S,匹配非空白字符,非\t\n\r空格等

import res = ‘axss\n\tsdf\t\r\t‘#匹配非\t\n\t空格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 remail = ‘123@163.com,@qq.com,123@.com,abd#163.com,abcd@163.com.cn‘#匹配邮箱必须有@符,@符前后得有[a-zA-Z0-9]必须出现一次,后缀可以是.com|.cn|.com.cnprint(re.findall(r‘(\w+@\w+(\.com\.cn|\.com|\.cn))‘, mail))   >>>输出[(‘123@163.com‘, ‘.com‘), (‘abcd@163.com.cn‘, ‘.com.cn‘)]

匹配url

import reurl = ‘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‘]

python-正则表达式

评论关闭