raghuprasad241 has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks,

Following is the excerpt from "Programming perl". I am having trouble wrapping my head around this regular expression and there is not much explanation to it in the book, can someone shed some light please? Just an fyi I tried my best to understand this but I am still not clear, sorry if I am asking a question that isn't supposed be asked here.

"You can do recursive patterns, too. One way is to have a compiled pattern that uses (??{ CODE }) to refer to itself. Recursive matching is pretty irregular, as regular expressions go. Any text on regular expressions will tell you that a standard regex can’t match nested parentheses correctly. And that’s correct. It’s also correct that Perl’s regexes aren’t standard. The following pattern matches a set of nested parentheses, however deep they go:

$np = qr{ \( (?: (?> [^()]+ ) # Non–parens without backtracking | (??{ $np }) # Group with matching parens )* \) }x;


You could use it like this to match a function call:

$funpat = qr/\w+$np/;<br> "myfunfun(1,(2*(3+4)),5)" =~ /^$funpat$/; #Matches!"


Jr. Monk

Replies are listed 'Best First'.
Re: Recursive regex
by LanX (Saint) on Feb 05, 2016 at 18:16 UTC

    $np matches everything between parens \( and \) which is

    • either not a paren in [^()]+
    • or something within parens (+ surrounding parens)
    in the latter case it descends into recursion and (?>...) avoids backtracking

    indentation helps:

    use re 'debug'; $np = qr{ \( (?: (?> [^()]+ ) # Non–parens without backtracking | (??{ $np }) # Group with matching parens )* \) }x; $funpat = qr/\w+$np/; "fun(1,(2),5)" =~ /^$funpat$/; #Matches!"

    and you might find this re-debug output helpful (just play with the input to have more concise informations)

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Je suis Charlie!

Re: Recursive regex
by AnomalousMonk (Archbishop) on Feb 05, 2016 at 18:14 UTC

    See also the discussion of the  (?PARNO) construct in Extended Patterns in perlre. This is available from Perl version 5.10 onward and does not have the scary "experimental" warning associated with it. It is preferable IMHO to  (??{ code }) where it can be used.

    I don't have time now for an extensive discussion of your original question, and I may not be the right person to deliver such discussion in any event. Perhaps someone else can jump in?


    Give a man a fish:  <%-{-{-{-<

Re: Recursive regex
by Laurent_R (Canon) on Feb 05, 2016 at 18:01 UTC
    It is looking recursively for matching nested parentheses. Please tell us what you don't understand in it.

    Update 18:15 UTC: sorry, I had to make the above short 15 minutes ago as my train was arriving in my home town. To be a bit more specific: $np is a regular expression looking for an opening parenthesis, followed by something, followed by a closing parenthesis, that something being any number of occurrences of either sequences of non parentheses characters, or patterns matched recursively by $np itself (i.e. more deeply nested sets of parens).

    Look at the example given, try it as it is and try it with an additional opening or closing parentheses, so that the parens no longer match. I guess you'll get it.

Re: Recursive regex
by raghuprasad241 (Beadle) on Feb 05, 2016 at 19:17 UTC
    Thank you every one for the response. Debug suggestion should help me in understanding this.

    Cheers!