Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

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

by choroba (Cardinal)
on Feb 11, 2020 at 21:24 UTC ( [id://11112818] : note . print w/replies, xml ) Need Help??


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

Sometimes, a parser is a better tool than a regex. I like Marpa::R2:
#!/usr/bin/perl use warnings; use strict; use feature qw{ say }; use Marpa::R2; my $dsl = << '__DSL__'; lexeme default = latm => 1 :default ::= action => ::first List ::= ('<') Items ('>') Items ::= Item action => list | Item (',') Items action => merge Item ::= List | digits digits ~ [\d]+ __DSL__ sub list { [ $_[1] ] } sub merge { [ $_[1], @{ $_[2] } ] } my $grammar = 'Marpa::R2::Scanless::G'->new({ source => \$dsl }); my @cases=('<1>', '<<1>>', '<1,2>', '<1,2,<3,4>,5,6>', '<<<1,2>,3>,<4,5,<6>>,<<7>,8,9>>'); for my $case (@cases) { my $value_ref = $grammar->parse(\$case, 'main'); use Data::Dumper; print Dumper $$value_ref; }

map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]