Search code examples
phpregextextile

Regular expression to match a block of text up to the first double new line?


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

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


Solution

  • Try this regex:

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

    meaning:

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

    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