python - How to convert variable to a regex string?
问题描述
I am working in python I am looping through a large group of strings and I want to be able to see if they are in a second list of strings.
for line in dictionary:
line = line.replace('\r\n','').replace('\n','')
for each in complex8list:
txt = re.compile(.*line.*)
if re.search(each, txt):
I need to be able to check if the string with anything before it, and anything after it is in the second list.
What is the correct syntax to do this?
解决方案
如果line
不是正则表达式,您甚至不需要正则表达式。
if line in each:
如果line
是一个正则表达式,那么你不需要做任何事情,因为前导.*
是隐含的,re.search
而尾随.*
是不必要的。
if re.search(line, each):
顺便说一句,您似乎有re.search
倒退的论据。
推荐阅读
- swift - 有没有办法以编程方式操作快捷方式标题
- javascript - Hide/show table rows with certain class with checkbox
- php - 在我尝试创建的 MVC PHP 应用程序中不断遇到 403
- c++ - strcmp/char* 转换未按预期工作
- c++ - 如何在 C++ 中获得随机函数的周期性
- windows - Windows Multipoint 占用端口 80
- wso2 - WSO2 EI - 数据源未连接到外部数据库,但数据服务连接
- r - ggplot中具有相同面板尺寸的gridExtra面板图
- visual-studio - MSBuild - 如何根据条件复制文件
- google-sheets-query - Google 工作表中工作簿之间的数据验证