Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: parse string containing space

by choroba (Cardinal)
on May 01, 2015 at 13:37 UTC ( [id://1125350]=note: print w/replies, xml ) Need Help??


in reply to parse string containing space

Build a parser. Marpa::R2 can help you with it:
#!/usr/bin/perl use warnings; use strict; use Marpa::R2; use Data::Dumper; my $input = q(name1=value1 name2=' value2=0' name3=value3); my $dsl = << '__DSL__'; lexeme default = latm => 1 :default ::= action => first List ::= Pair action => single | Pair white List action => store Pair ::= Key '=' Value action => pair Key ::= noneq Value ::= word | Quoted Quoted ::= quote string quote action => second noneq ~ [^=]+ word ~ [^\s]+ string ~ [^']+ white ~ [\s]+ quote ~ ['] __DSL__ sub first { $_[1] } sub second { $_[2] } sub single { [ $_[1] ] } sub pair { +{ $_[1] => $_[3] } } sub store { [ $_[1], @{ $_[3] } ] } my $grammar = 'Marpa::R2::Scanless::G'->new( { source => \$dsl } ); my $value_ref = $grammar->parse( \$input, 'main' ); print Dumper $value_ref;

Output:

$VAR1 = \[ { 'name1' => 'value1' }, { 'name2' => ' value2=0' }, { 'name3' => 'value3' } ];
لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Replies are listed 'Best First'.
Re^2: parse string containing space
by mtovey (Initiate) on May 01, 2015 at 19:46 UTC

    Two good possibilities! I will try to test this weekend and see what shakes out.

    Thanks!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2024-09-15 11:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    The PerlMonks site front end has:





    Results (21 votes). Check out past polls.

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.