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
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.