这是新手。
我想在一个Python脚本中将多个正则表达式批处理在一起。
当正则表达式通过Notepad ++中的文本文件进行搜索时,每个正则表达式都能正常工作。
但是,当结合使用时,它不起作用。不起作用意味着我的Python脚本未返回在Notepad ++搜索中找到的有效匹配。
运行Python 3.7。
先谢谢了。
# import Python's regular expression module
import re
# file to check; assumes target file is in the same dir as Python script
target = 'test target file.txt'
# declare series of regexes; see top for explanation of each
regexes = [re.compile('^\+'),
re.compile('^[\s+][A-Z]'),
re.compile('\d$\r\n[cm,%,\),l,ml,hPa,°,bar,psi,V,W,]'),
re.compile('^[1-9]{1,2}\.(.*)\r\n^\r\n^[1-9]{1,2}\.(.*)'),
re.compile('FN-'),
re.compile('gfx'),
re.compile('tbl'),
re.compile('Missing link'),
]
# open target file, search for matches, output to screen
with open(target) as fp:
for line in target:
for cnt, line in enumerate(fp):
if any(regex.match(line) for regex in regexes):
print("Line {}: {}".format(cnt, line))