[英]How to match all characters inlcuding new lines in between two words using regex in python?
for example I have something like:例如我有类似的东西:
OLD
WHO IS THIS
END?
On Sun, 22 Sep 2019 at 23:57,
I'd like to capture everything from the beginning till \n\nOn Sun,
我想从头到尾捕捉到\n\nOn Sun,
so my expected outcome would be:所以我的预期结果是:
OLD
WHO IS THIS
END?
I tried doing the following pattern: ^(.*)\n\nOn.* wrote:
which only works if the text I need doesn't contain new lines.我尝试执行以下模式: ^(.*)\n\nOn.* wrote:
仅当我需要的文本不包含新行时才有效。 I'd really appreciate any help.我真的很感激任何帮助。 Thanks.谢谢。
No need for Regex try this:不需要正则表达式试试这个:
text = """
OLD
WHO IS THIS
END?
On Sun, 22 Sep 2019 at 23:57,
"""
text = '\n'.join(line for line in text.split('\n') if line.startswith('On Sun'))
print(text)
Output: Output:
OLD
WHO IS THIS
END?
The direct translation would be直接翻译是
^OLD.+?(?=^On Sun)
See a demo on regex101.com (mind the multi- and singleline flags).请参阅regex101.com 上的演示(注意多行和单行标志)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.