简体   繁体   English

python 中的正则表达式多行匹配

[英]regex multiline matching in python

I want to filter for ' here is a sample ' and all the lines afterwards until 2 new lines :我想过滤“这里是一个样本”以及之后的所有行,直到2 个新行

Here is my file (you can use it as a logfile):这是我的文件(您可以将其用作日志文件):

here is a sample text
random line1


here is a sample text
random line2
random line3
random line4


should not match
random line 6


here is a sample 
random line 5

I tried:我试过了:

    \r?\n?(here is a sample).*\r?\n?(.*)

With that I only filter the next line if I do the last part '\r?\n?(.*)' again I get another line..这样,如果我再次执行最后一部分'\r?\n?(.*)' ,我只会过滤下一行,我会得到另一行..

My question.我的问题。 What regex expression do I need in order to match all lines until I see 2 new lines .我需要什么正则表达式才能匹配所有行,直到我看到 2 个新行

If you want to match all until you have 2 newline, but also want to match the last occurrence if there are no 2 newlines:如果你想匹配所有直到你有 2 个换行符,但如果没有 2 个换行符也想匹配最后一次出现:

^here is a sample.*(?:\n(?!\n).*)*

The pattern matches:模式匹配:

  • ^ Start of string ^字符串开头
  • here is a sample.* Match literally and the rest of the line here is a sample.*匹配字面意思和该行的 rest
  • (?: Non capture group to repeat as a whole part (?:非捕获组作为一个整体重复
    • \n(?!\n) Match a newline, and assert that it is not directly followed by a newline \n(?!\n)匹配一个换行符,并断言它后面不直接跟一个换行符
    • .* Match the rest of the line .*匹配线的rest
  • )* Close the non capture group and optionally repeat it )*关闭非捕获组并选择性地重复它

Regex demo正则表达式演示

If there should be 2 newlines present, you can use a capture group for the part that you want to keep, and match the 2 newlines to make sure that they are present.如果应该存在 2 个换行符,您可以为要保留的部分使用捕获组,并匹配 2 个换行符以确保它们存在。

^(here is a sample.*(?:\n(?!\n).*)*)\n\n

Regex demo正则表达式演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM