Why is my PHP regex that parses Markdown links broken?

前端 未结 2 1810
悲哀的现实
悲哀的现实 2020-12-31 22:27
$pattern = \"/\\[(.*?)\\]\\((.*?)\\)/i\";
$replace = \"$1\";
$text = \"blah blah [LINK1](http://example.com)          


        
2条回答
  •  傲寒
    傲寒 (楼主)
    2020-12-31 22:56

    If I understand you right, all you need to do really is also match any number of spaces between the two as well, for example:

    /\[([^]]*)\] *\(([^)]*)\)/i
    

    Explanation:

    \[             # Matches the opening square bracket (escaped)
    ([^]]*)        # Captures any number of characters that aren't close square brackets
    \]             # Match close square bracket (escaped)
     *             # Match any number of spaces
    \(             # Match the opening bracket (escaped)
    ([^)]*)        # Captures any number of characters that aren't close brackets
    \)             # Match the close bracket (escaped)
    

    Justification:

    I should probably justify that the reason I changed your .*? into [^]]*

    The second version is more efficient because it doesn't need to do a huge amount of backtracking that .*? does. Additionally, once an opening [ is encountered, the .*? version will carry on looking until it finds a match, rather than failing if it is not a tag as we would want. For example, if we match the expression using .*? against:

    Sad face :[ blah [LINK1](http://sub.example.com/) blah
    

    it will match

    [ blah [LINK1]
    

    and

    http://sub.example.com/
    

    Using the [^]]* approach will mean that the input is matched correctly.

提交回复
热议问题