我正面临一个相当难以捉摸的错误,这似乎是由于从文件中读取而引起的。 我简化了我的程序来演示这个问题:
认为这个程序运行良好:
import re
sourceString="Yesterday I had a pizza for lunch it was tasty\n";
sourceString+="today I am thinking about grabbing a burger and tomorrow it\n";
sourceString+="will probably be some fish if I am lucky\n\n\n";
sourceString+="see you later!"
jj=["pizza","steak","fish"]
for keyword in jj:
regexPattern= keyword+".*";
patternObject=re.compile(regexPattern,re.MULTILINE);
match=patternObject.search(sourceString);
if match:
print("Match found for "+keyword)
print(match.group()+"\n")
else:
print("warning: no match found for :"+ keyword+"\n")
我正在使用一个非常简单的正则表达式模式,但我从我的数组jj
获得正则表达式的要点
脚本按预期工作(匹配模式包含"披萨""鱼"但不匹配"牛排")
现在在我的实际程序中,我试图从文件中读取这些关键字(我不想在源代码中进行硬编码)
到目前为止,我有这个:import re
sourceString="Yesterday I had a pizza for lunch it was tasty\n";
sourceString+="today I am thinking about grabbing a burger and tomorrow it\n";
sourceString+="will probably be some fish if I am lucky\n\n\n";
sourceString+="see you later!"
with open('keyWords.txt','r') as f:
for keyword in f:
regexPattern= keyword+".*";
patternObject=re.compile(regexPattern,re.MULTILINE);
match=patternObject.search(sourceString);
if match:
print("Match found for "+keyword)
print(match.group())
else:
print("warning: no match found for :"+ keyword)
其中keyWords.txt将包含以下内容:
pizza
steak
fish
但是这会破坏代码,因为只有文件中的LAST关键字才能成功匹配(如果存在匹配)。
是什么给出了?