Nested brackets

Michel Fortin michel.fortin at michelf.com
Fri Sep 2 11:42:34 EDT 2005


Le 2005-09-02 à 9:32, Eric Daspet a écrit :


> I've never tried it but if I trust PHP documentation, recursive

> patterns are allowed in PHP (search "Recursive patterns" in http://

> fr.php.net/manual/en/reference.pcre.pattern.syntax.php)


Pretty interesting indeed. I haven't seen that before. Thank you Eric.

I have made some tests and I'm pleased to announce it works like a
charm on PHP 4.3.11. Here is the new nested regex:

$md_nested_brackets = '(?P<n_brakets>(?>(?>[^\[\]]+)|\[(?
P>n_brakets)\])*)';

You also need to remove parenthesis around `$md_nested_bracket` in
every regex that use it through the program, because `
$md_nested_brackets` now include a capturing parenthesis of its own.

What is unfortunate is that I can't find a way to make this work with
PHP version prior than 4.3.3. Here is what the PHP documentation says
about it:

> Since PHP 4.3.3, (?1), (?2) and so on can be used for recursive

> subpatterns too. It is also possible to use named subpatterns: (?

> P>foo).

Hense, in older versions, I can only match the whole pattern
recursively. Since an anchor inside an anchor inside an anchor isn't
really what we want, and since PHP Markdown requirement is currently
PHP 4.1 or later, I think I'll skip this improvement -- for now. This
could work in Python, I don't know.


Michel Fortin
michel.fortin at michelf.com
http://www.michelf.com/




More information about the Markdown-Discuss mailing list