本文介绍了正则表达式将文本块匹配到第一双新行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个简单的Textile解析器,并试图为"blockquote"编写一个正则表达式,但是很难匹配多个新行.示例:

I'm making a simple Textile parser and am trying to write a regular expression for "blockquote" but am having difficulty matching multiple new lines. Example:


bq. first line of quote
second line of quote
third line of quote

not part of the quote

它将通过preg_replace()替换为blockquote标记,因此基本上它需要匹配"bq."和它遇到的第一双新行之间的所有内容.我能管理的最好的办法就是获得报价的第一行.谢谢

It will be replaced with blockquote tags via preg_replace() so basically it needs to match everything between "bq." and the first double new line it comes across. The best I can manage is to get the first line of the quote. Thanks

推荐答案

尝试此正则表达式:

(?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

这篇关于正则表达式将文本块匹配到第一双新行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-27 06:42
查看更多