Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
380 views
in Technique[技术] by (71.8m points)

php - 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try this regex:

(?s)bq.((?!(
?
){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
      
?      #       match the character '
' and match it once or none at all
      
       #       match the character '
'
    ){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 ? matches a Windows, *nix and (newer) MacOS line breaks. If you need to account for real old Mac computers, add the single to it: ? |


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...