我有一项任务是创建一个将获取数据并将其放入日志文件的任务。我有这个示例文本文件,它包含这样的块/行。
START OF SAMPLE
lines of sample
lines of sample
lines of sample
END OF SAMPLE
START OF SAMPLE
lines of sample
lines of sample
lines of sample
linSTART OF SAMPLE
lines of sample
lines of sample
lines of sample
START OF SAMPLE
lines of sample
lines of sample
lines of sample
END OF SAMPLE
正如你所看到的,样本中有一个破碎的块。我想找到它并用这样的东西替换它:
START OF SAMPLE
Corrupted SAMPLE
END OF SAMPLE
即使该块中没有END OF SAMPLE,我也希望这样......
答案 0 :(得分:0)
以下正则表达式将匹配所有有效的样本。之后,您可以根据需要对此数据执行任何操作。
START OF SAMPLE(?:(?!START OF SAMPLE).)+?END OF SAMPLE
以下正则表达式需要DOTALL标志。有关更多数据,请阅读python中正则表达式的the documentation。