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

前端 未结 5 652
面向向阳花
面向向阳花 2020-12-11 20:41

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:

         


        
5条回答
  •  暖寄归人
    2020-12-11 20:49

    My instincts tell me something like...

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

    Just like the above fella says, the s flag after the / at the end of the RegEx means that the . will match new line characters. Usually, without this, RegExs are kind of a one line thing.

    Then the question mark ? after the .+ denotes a non-greedy match so that the .+ won't match as it can; instead it will match the minimum possible, so that the \n\n will match the first available double line.

    To what extent are you planning on supporting features of Textile? Because your RegEx can get pretty complicated, as Textile allows things like...

    bq.. This is a block quote
    
    This is still a block quote
    

    or...

    bq(funky). This is a block quote belonging to the class funky!
    
    bq{color:red;}. Block quote with red text!
    

    All of which your regex-replace technique won't be able to handle, methinks.

提交回复
热议问题