Regular expression to match block of text with first double newline?

I'm making a simple Textile parser and trying to write a regex for "blockquote", but it's hard for me to match a few newlines. Example:

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

not part of the quote

It will be replaced by blockquote tags through preg_replace(), so basically it needs to match everything between "bq."and the first double new line that it encounters. The best I can do is get the first line of the quote. Thanks

+3
source share
5 answers

Try this regex:

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

value:

(?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 (newer). Mac, \r: \r?\n|\r

+6

. :

$text =~ /(?s)bq\.(.+?)\n\n/g
+1

?

'/(.+)\n\n/s'

, , .

0

- ...

preg_match("/^bq\. (.+?)\n\n/s", $input, $matches)

, s / RegEx , . . , , RegEx .

? .+ , .+ , ; , \n\n .

Textile? RegEx , Textile , ...

bq.. This is a block quote

This is still a block quote

...

bq(funky). This is a block quote belonging to the class funky!

bq{color:red;}. Block quote with red text!

, , .

0

Edit: Ehr, ask the wrong question .. "bq." was significant.

echo preg_replace('/^bq\.(.+?)\n\n/s', '<blockquote>$1</blockquote>', $str, 1);

Sometimes the data input through webforms contains \ r \ n instead of just \ n what would it do

echo preg_replace('/^bq\.(.+?)\r\n\r\n/s', '<blockquote>$1</blockquote>', $str, 1);

The question mark adds closing locks after detecting the first double return ("not greedy" in my opinion, it is called), so any other double returns remain valid (if this is not what you want, print it, obviously).

0
source

Source: https://habr.com/ru/post/1731868/


All Articles