接着昨天的内容今天探讨,优美匹配和视前和视后匹配
1.格式化匹配
import re reg = r‘‘‘(?x)(\d{4})? #匹配前四个 支持注释 (-)? (\d{8}) ‘‘‘ num = ‘0571-88982571, 0572-05975867,057269781578, 0539-66862954, 56788888‘ pat = re.findall(reg, num) #print(pat) for each in pat: print(‘‘.join(each))#连接
>>>
0571-88982571 0572-05975867 057269781578 0539-66862954 56788888
2.视前匹配 查找匹配项,但匹配项不显示在结果中,而是返回(?=)前面的匹配项
import re reg = r‘.+(?= ming lili)‘ s = ‘‘‘ hello ming lili python welcome to home ming lili java hello python xxio ‘‘‘ pat = re.findall(reg, s) print(pat)
>>>
[‘ hello‘, ‘ welcome to home‘]
3.视后匹配 没搞明白,上个例子
import re reg = r‘\s+(?!ming|lily)(\w+)‘ s =‘‘‘ sales@hello.com ming@qq.com python@wexin.com lily@hello.com ‘‘‘ pat = re.findall(reg, s) print(pat)
>>>
[‘\nhello‘, ‘world‘, ‘welcome‘, ‘to‘, ‘home ‘]