且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

正则表达式将文本块匹配到第一双新行?

更新时间:2021-12-14 18:08:05

尝试此正则表达式:

(?s)bq\.((?!(\r?\n){2}).)*+

含义:

(?s)           # enable dot-all option
b              # match the character 'b'
q              # match the character 'q'
\.             # match the character '.'
(              # start capture group 1
  (?!          #   start negative look ahead
    (          #     start capture group 2
      \r?      #       match the character '\r' and match it once or none at all
      \n       #       match the character '\n'
    ){2}       #     end capture group 2 and repeat it exactly 2 times
  )            #   end negative look ahead
  .            #   match any character
)*+            # end capture group 1 and repeat it zero or more times, possessively

\r?\n与Windows,* nix和(较新的)MacOS换行符匹配.如果您需要考虑使用真正的旧Mac计算机,请在其中添加单个\r:\r?\n|\r

The \r?\n matches a Windows, *nix and (newer) MacOS line breaks. If you need to account for real old Mac computers, add the single \r to it: \r?\n|\r