需求一、正则过滤匹配结果可以用(xxx),该用法为捕获组
直接对感兴趣的部分直接加上括号即可
# 捕获邮箱类型
line = 'nihao123@163.com'
res = re.findall('.*@(.*)\.com', line)
print(res)
>>['163']
需求二、将括号内的规则视为一个整体用作回溯引用,该用法为非捕获组
需要在括号的最前加上?: (?:xxx)
# 匹配C语言类型的注释
text1 = '/* this is a comment */'
text2 = '''/* this is a
multiline comment */... '''
# 点号不能匹配换行符
res = re.findall('/\* (?:.|\n)*? \*/', text2)
print(res)
注意;
