python - 重新匹配 ('any item from a list' ) 与 python

标签 python regex

我正在尝试概括一个 python 脚本,该脚本旨在分析三种特定植物,以便能够分析任何种类的物种。我有一段代码:

dump=open('dump')
for i in dump:
  if re.match('CON|VEN|PLE',i):
     print i

我想说:

dump=open('dump')
list = ['CON','PLE','VEN']
for i in dump:
  if re.match('any item from my list',i): # How can I ask for item from a list that match?
     print i

因为我必须使用来自不同列表的大量不同 ID 多次重现此代码。

最佳答案

i 是您文件中的一行,因此为了清楚起见,我将其称为 line

 if set(line.split()).intersection(list_of_words)

如果你真的想用正则表达式来做

 if re.find("|".join(sorted(list_of_words,key=len,reverse=True)),line)

应该工作...

关于python - 重新匹配 ('any item from a list' ) 与 python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23787809/

相关文章:

python - web.py session 根本不起作用

python - 如何更改标签、行编辑等相对于窗口大小的位置?

jquery - jquery 表单验证中的错误消息

c# - .NET 正则表达式错误 : [x-y] range in reverse order

c# - 正则表达式模式帮助

python - 阻止 Pandas Dataframe 'float' 对象没有属性 'split'

python - 如何将 sns.facetgrid 保存为 pdf

JavaScript regex exec 执行时间太长

python - 如何评估 re.sub 中的替换零件?

python - 将网格化数据插值到地理位置