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

Matching a keyword, value and optional comma delimited values

by brainpan (Monk)
on Dec 21, 2000 at 20:21 UTC ( [id://47919]=perlquestion: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.

brainpan has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to match a string like the following:

keyword = value, optional1, optional2, optional3

where only keyword and value are required, and there are zero or more optional parameters.

So far all I've been able to come up with is

/^(\w+)\s*=\s*(\w+)([,[\s\w]*]*)$/

The above regex works, but it seems like I should be able to have $3 return without commas in it. Is there a way to do this w/out resorting to using something like join(' ', split(/,/, $3)) to process the results of the regex?

Replies are listed 'Best First'.
Re: Matching a keyword, value and optional comma delimited values
by mirod (Canon) on Dec 21, 2000 at 20:33 UTC

    A dirty solution:

    ($keyword, $value, @opt)= split /\s*[=,]\s*/;

    It works only if you have no extra = or , in your data.

Re: Matching a keyword, value and optional comma delimited values
by chipmunk (Parson) on Dec 21, 2000 at 20:39 UTC
    There is no way to get $3 without the commas in it, because each application of a parenthesized group has to match a continuous substring. You will have to do this in two steps. Here's another two step solution:
    ($keyword, $value, @optional) = split /\s*[=,]\s*/; $optional = join ' ', @optional;
Re: Matching a keyword, value and optional comma delimited values
by dws (Chancellor) on Dec 21, 2000 at 23:38 UTC
    Divide and conquer:
    my ($keyword, $values) = m/^(\w+)\s*=\s*(.*)$/;
    my @values = split(/,\s*/, $values);
    
Re: Matching a keyword, value and optional comma delimited values
by BooK (Curate) on Dec 21, 2000 at 23:39 UTC
    You can try those:
    • if you have not more than 3 optional values:
      # match everything at once @everything = /^\s*(\w+)\s*=\s*(\w+)(\s*,\s*\w+)?(\s*,\s*\w+)?(\s*,\s* +\w+)?$/
    • if you can have any number of optional values, you have to use \G (explained in perlop and perlre)
      # match the required part /^\s*(\w+)\s*=\s*(\w+)/gc; ($keyword, $value) = ($1,$2); # match the optional part @optional = /\G\s*,\s*(\w+)/g;
Re: Matching a keyword, value and optional comma delimited values
by OeufMayo (Curate) on Dec 22, 2000 at 10:32 UTC
    Not sure if it's what you're looking for, but here's a all-in-one regex that can do the trick:
    $_="keyword=value,option1,option2,option3"; s{^(\w+) \s*=\s* (.+)$ } { @a=split(',',$2) }ex

    You now have the keyword in $1, and the values in the array @a

    <kbd>--
    PerlMonger::Paris(http => 'paris.pm.org');</kbd>
Re: Matching a keyword, value and optional comma delimited values
by runrig (Abbot) on Dec 22, 2000 at 16:24 UTC
    You could always take $3 and do something like:
    my $opts = $3; $opts =~ s/\s*,\s*//g;
    I think I'd rather do the whole thing in more than one step though, so if there were spaces IN the optional args you have more control over them, and the thing looks simpler:
    #!/usr/local/bin/perl -l -w use strict; my $str="abc = def, ghi, jkl"; my ($req, $opts) = split /\s*,\s*/, $str, 2; $opts = '' unless defined $opts; my ($key, $value) = split /\s*=\s*/, $req; my @opts = split /\s*,\s*/, $opts; print qq(key="$key"); print qq(val="$value"); print join(",", map {qq("$_")} @opts);

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://47919]
Approved by root
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.