Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Re: regex for nested "<"/">'

by TheDamian (Priest)
on Feb 12, 2020 at 09:31 UTC ( #11112852=note: print w/replies, xml ) Need Help??

in reply to regex for nested "<"/">'

What have I got wrong?

You mistranscribed that \d*+.
It should be \d++.
(I checked back through all of my notes and presentations, and its definitely shown as \d++ in them.)

The nested match goes wrong when you use \d*+, because when it encounters a nested  < the \d*+ successfully matches zero times.

And, because the \d*+ | (?&LIST) is itself in a non-backtracking loop: (\d*+ | (?&LIST) )*+, when the zero-length submatch causes the main match to fail, the regex engine can't backtrack into the alternative and try the (?&LIST) instead.

So the original match from the start of the string fails, and the regex engine skips down the string, trying again and again, until it finds the nested sublist, which it is able to match.

Incidentally, you could have watched this happen live and in colour via the Regexp::Debugger module. Just download it from CPAN and add it in front of your regex:

use Regexp::Debugger; my $re=qr{(?x) (?&LIST) (?(DEFINE) (?<LIST> < (?&ITEM) (?: , (?&ITEM))*+ > ) (?<ITEM> \d*+ | (?&LIST) ) ) };

Replies are listed 'Best First'.
Re^2: regex for nested "<"/">'
by shadowsong (Pilgrim) on Feb 12, 2020 at 22:26 UTC

    Just started reading Perl Best Practices, it's a real gem.

    Thank you, Sir.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11112852]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (8)
As of 2022-08-18 08:10 GMT
Find Nodes?
    Voting Booth?

    No recent polls found