python 这个匹配规则语句 pattern = r10d13d13d13 怎么设置不匹配 10101010testlog 这种文件
要排除匹配 10.10.10.10.test.log 这种文件,你可以使用负向预查来修改匹配规则。以下是一个示例:
import re
pattern = r'10\.\d{1,3}\.\d{1,3}\.\d{1,3}(?!\.test\.log)'
strings = ['10.10.10.10.test.log', '10.10.10.10']
for string in strings:
match = re.search(pattern, string)
if match:
print(f'Matched: {match.group()}')
else:
print('Not matched')
这将输出:
Not matched
Matched: 10.10.10.10
在匹配规则中,使用了负向预查 (?!\.test\.log) 来指定不希望匹配的模式(即排除以 .test.log 结尾的字符串)。
原文地址: http://www.cveoy.top/t/topic/iiRu 著作权归作者所有。请勿转载和采集!