Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

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) ) ) };
Damian

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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2020-04-01 09:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    To "Disagree to disagree" means to:









    Results (186 votes). Check out past polls.

    Notices?