我希望在Python中使用正则表达式来读取文本,查找与标记位于同一句子中的所有实例,然后允许将这些句子打印到输出文件的唯一行:
import re
out = open('out.txt', 'w')
readfile = "<location> Oklahoma </location> where the wind comes <emotion> sweeping </emotion> down <location> the plain </location>. And the waving wheat. It can sure smell <emotion> sweet </emotion>."
for match in re.findall(r'(?:(?<=\.)\s+|^)((?=(?:(?!\.(?:\s|$)).)*?\bemotion>(?=\s|\.|$))(?=(?:(?!\.(?:\s|$)).)*?\blocation>(?=\s|\.|$)).*?\.(?=\s|$))', readfile, flags=re.I):
line = ''.join(str(x) for x in match)
out.write(line + '\n')
out.close()
问题是,如果我读入一个包含换行符的文件,正则表达式就会失败:
import re
out = open('out.txt', 'w')
readfile = "<location> Oklahoma </location> where the wind \n comes <emotion> sweeping </emotion> down <location> the plain </location>. And the waving wheat. It can sure smell <emotion> sweet </emotion>."
for match in re.findall(r'(?:(?<=\.)\s+|^)((?=(?:(?!\.(?:\s|$)).)*?\bemotion>(?=\s|\.|$))(?=(?:(?!\.(?:\s|$)).)*?\blocation>(?=\s|\.|$)).*?\.(?=\s|$))', readfile, flags=re.I):
line = ''.join(str(x) for x in match)
out.write(line + '\n')
out.close()
是否有任何方法可以修改此正则表达式,使其在命中时不会阻塞\n?如果其他人能就这个问题提供任何建议,我将不胜感激。你知道吗
使用
re.DOTALL
/re.S
将re^{} or ^{} (它们是相同的东西)添加到正则表达式中的标志中。这将导致
.
也匹配换行符。所以flags
参数的新值是re.I | re.S
。你知道吗相关问题 更多 >
编程相关推荐