Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
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]

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (1)
As of 2024-04-25 04:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found