Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

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

by choroba (Archbishop)
on Feb 11, 2020 at 21:24 UTC ( #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]

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2020-07-12 03:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?