This page is outdated. See Recursive patterns for a native implementation.
Sometimes, you need your regexp to do more than PCRE can provide. For example, if you were to try to do brace matching with regexp, you'll soon find it's simply impossible to do such a thing (unless you're using perl6, in which case you should see this page).
What's this ?
First, what's brace matching ?
Let's say you want to find all texts between {} ; for instance for LaTeX-like support.
So you'll write something like #{(.+)}#U.
If your input is Some {input provided} by the user, everything will be fine and you'll get the expected "input provided".
But let's say your user write this : Some {input {provided} and encapsulated} by the user. This time, all you'll get is "input { provided". Which is obviously an error...
Example
This page covers a solution for this problem. With the code below, heavy-braced input like this one (who even crash GeShi !):
... will be translated to the HTML result you are looking for :
Solution
The trick is to override the default function (PHP preg_replace in this case, although the solution can be adapted to any language) and to look for any braces. This solution is clearly incomplete and simple, but you may build more complex layer of code on it once you understand the principles behind.
Limitation
This piece of code will only work if you're looking for one pair of brace and no more : codes such as this one #{(.+)}{(.+)#U will result in unpredictable behavior. If you want to match such case, you'll need to code your solution... or to improve this one.
Examples
This snippet is used on Le Typographe, a french application for text formatting.