python - Python 脚本中的多个正则表达式搜索。
问题描述
我对这一切都很陌生。
我想在一个 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))
解决方案
推荐阅读
- c# - 如何访问在 startup.cs 中定义的 program.cs 中的单例服务
- extjs - 如何在使用其参考选择的按钮上触发事件
- c++ - 在 Visual Studio 2019 的 linux 项目的链接器中设置静态库 (libconfig.a) 的路径
- node.js - 与 Sapper 兼容的设置,可从单个 Express/Polka 入口点为多个应用程序提供服务
- c# - 如何使用反射动态传递表名
- java - 与 ProcessBuilder 一起更改环境变量的问题
- reactjs - React-testing-library 用产生警告
- c# - 如何使用带有 OData 的 Automapper Queryable 扩展,嵌套 $select
- r - 我们如何检测在 R 中具有不同结果的观察结果?
- spring-boot - 如何在 Thymleaf 中动态设置数字类型的最大值