import re
re.findall(pattern, string, flags=0)
pattern 指的是,正则表达式匹配规则
string 指的是,要进行匹配的字符串
flags 指的是,可选参数,进行特定条件的匹配,如能让匹配时不区分大小写的re.I和能让元字符.匹配\n的re.S
\的意思:将下一个字符标记为一个特殊字符、或一个原义字符、或一个向后引用、或一个八进制转义符。
match_str = 'bac | bbc | bcc | bdc | bec'
字符集
print(re.findall('b[ce]c',match_str))
取反
print(re.findall('b[^ce]c',match_str))
精简形式
print(re.findall('b[a-d]c',match_str))
取反
print(re.findall('b[^a-d]c',match_str))
match_str = '&a0b12c344d55&AC_'
常见元字符--匹配数字
print(re.findall('\d',match_str))
print(re.findall('\d\d',match_str))
取反
print(re.findall('\D',match_str))
单词字符,字母数字下划线
match_str = '&a0b12c344d55&AC_'
print(re.findall('\w',match_str))
取反
print(re.findall('\W',match_str))
空白字符
match_str = '\r\t&a0b12c344d55&AC_ \n'
print(re.findall('\s',match_str))
取反
print(re.findall('\S',match_str))
数量词,限定匹配次数{}
match_str = '&a0b12c344d55&AC_678911'
print(re.findall('\d{1,4}',match_str))
match_str2 = 'lemon12banana34pear56'
pat='[a-z]{1,}'
print(re.findall(pat,match_str2))
print(re.findall('[a-z]{1,}',match_str2))
贪婪与非贪婪 ?按最小长度匹配,默认最大长度
match_str = 'lemon12banananan34pear56'
print(re.findall('[a-z]{4,6}',match_str))
print(re.findall('[a-z]{4,6}?',match_str))
次数匹配
? 代表匹配前面的字符零次或一次 {0,1}
* 代表匹配前面的字符零次或多次 {0,}
+ 代表匹配前面的字符一次或多次 {1,}
match_str = 'lemo123 lemon345 lemonnnn567'
print(re.findall('lemon*',match_str))
print(re.findall('lemon+',match_str))
print(re.findall('lemon?',match_str))
对于 ? 总结
1.如果数量词后面有 ?即{}后有?,该 ? 代表非贪婪的关键字,倾向于取最小长度匹配
2.如果字符 后面有 ?,该 ? 代表匹配前面字符 0次或1次
自己练习次数匹配1
match_str = 'lemon12ba3le4leaderleadinglabornananan34pear56'
print(re.findall('le[a-z]?',match_str)) 2---3位,从第2位开始的字符都要在a-z内
print(re.findall('le[a-z]*',match_str)) 2---无数位,从第三位开始的字符都要在a-z内
print(re.findall('le[a-z]+',match_str)) 3---无数位,从第3位开始的字符都要在a-z内
print(re.findall('le[a,b,c,d,e,f]+',match_str)) 3---无数位,从第3位开始的字符都要在a-f内
自己练习次数匹配2
match_str = 'lemon12ba3le4leaderleadinglabornananan34pear56'
print(re.findall('le[a-z]?',match_str))
print(re.findall('[a-z]?',match_str)) 这说明字符集[a-z]前有字母时,匹配显示的空我们只是看不出来而已,当删掉字符集[a-z]前面的字母时,匹配的空一下就显示出来了
定位符(用来匹配字符串的边界)
^匹配 字符串开始的位置
$匹配 字符串结尾的位置
match_str = 'abcdef 123456 abcdef 456 abc'
print(re.findall('^[a-z]*',match_str)) 指定 被匹配的字符串的开始位置必须是字母
match_str2 = '1abcdef 123456 abcdef 456 abc'
print(re.findall('^[a-z]*',match_str2)) 被匹配的字符串的开始位置不是字母,匹配不到
print(re.findall('[a-z]{3}$',match_str))
组的匹配()
组 与 字符集 区别 : (lemon) 匹配 lemon 这一组字符 ; [lemon] 匹配 括号中的任意一个字母
match_str = 'lemonlemonlemonappleapplepearpear'
print(re.findall('(lemon){3}',match_str)) findall进行组的匹配时会降重
print( re.search('(lemon){3}',match_str).group() )
. re.I re.S 可选参数
match_str = 'lemon\n LEMON\n'
print(re.findall('.',match_str)) .能匹配除“\n”之外的任何单个字符
print(re.findall('.',match_str,re.S)) 能让元字符.匹配\n的re.S
print(re.findall('(lemon.)',match_str,re.S|re.I)) 能让匹配时不区分大小写的re.I和能让元字符.匹配\n的re.S
re.findall 搜索整个字符串,返回所有匹配项。
re.search 搜索整个字符串,若匹配则返回第一个匹配对象,若全都不匹配,则返回None。
re.match 从字符串首字符开始匹配,若首字符不匹配,则返回None,若匹配则返回第一个匹配对象。
match_str = 'a5678 lemon 1234'
match_str2='a5678 lemon 1234'
print(re.findall('\d',match_str))
print(re.search('\d',match_str2).group())
print(re.match('\d',match_str2)) 匹配成功后,注意成功后,可以用.group()查看对象
group组匹配
group(0) 记录的是完整匹配的结果
用 () 来表示其中的内容属于一个分组
match_str = 'life is mostly happy,but sometimes sad'
需求:
1.将 is mostly happy,but sometimes 匹配出来
2.将 is mostly happy 以及 sometimes 分别匹配出来
pat1='life(.*)sad'
r1=re.findall(pat1,match_str) 返回待匹配的内容
print(r1)
pat2='life(.*),but(.*)sad'
r2=re.search(pat2,match_str)
print(r2.group(0)) 整句话
print(r2.group(1)) 第一组
print(r2.group(2)) 第二组
print(r2.groups()) 所有组
re.sub(pattern, repl, string, count=0, flags=0)
pattern 正则表达式
repl 要替换的内容,也可传入函数
string 被替换的字符串
count 默认为0,代表全部替换 。
1 代表替换1次,
2 代表替换2次,以此类推
match_str = 'lemon apple 123456789 lemon lemon'
需求:将lemon 全都转为 a
print(re.sub('lemon','a',match_str,count=0))
需求 xx:数字 小于 7的,都转为 0 , 数字 大于等于 7的,都转为 10
def transfer(value):
match_num=value.group()
print(match_num)
if int(match_num)<7:
return'0'
return'10'
print(re.sub('\d',transfer,match_str,count=0))
自己练习,把电话号码的中间四位换为*号
telephone='13152498967;13545689962;18809872345'
pat='\d{3}\d{4}\d{4}'
print(re.findall(pat,telephone))