hv has asked for the wisdom of the Perl Monks concerning the following question:
I'm writing code to parse a simplish string that has components like:
and can also have a separating ':' somewhere in there.bareword "quoted string" 'quoted string' bareword = bareword bareword = "quoted string" bareword = 'quoted string'
My code for this currently looks like this:
while ($tag =~ m{
(?:
Update:
but that feels like an ugly way to do things - there's duplication of chunks of the pattern, and all those assumptions about the capture numbering. Surely there must be a better way to do this? Hugowhile ($tag =~ m{ \G (?: ( \w+ ) (?: \s* = \s* (?: ( \w+ ) | ' ([^']*) ' | " ([^"]*) " ) )? | ' ([^']*) ' | " ([^"]*) " | ( : ) ) (?= \s | \z) \s* ) }gcxs) { push @args, defined($5) ? $5 # 'quoted string' : defined($6) ? $6 # "quoted string" : defined($7) ? $7 # : : defined($2) ? [ $1, $2 ] # bareword=bareword : defined($3) ? [ $1, $3 ] # bareword='quoted string' : defined($4) ? [ $1, $4 ] # bareword="quoted string" : $1 # bareword ; }
Update per author - dvergin 2003-02-21
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Parsing arguments
by tadman (Prior) on Feb 20, 2003 at 17:26 UTC | |
by hv (Prior) on Feb 20, 2003 at 17:42 UTC | |
|
Re: Parsing arguments
by xmath (Hermit) on Feb 20, 2003 at 18:52 UTC | |
by hv (Prior) on Feb 20, 2003 at 19:15 UTC | |
by xmath (Hermit) on Feb 20, 2003 at 19:32 UTC | |
by hv (Prior) on Feb 20, 2003 at 19:55 UTC | |
|
Re: Parsing arguments
by Thelonius (Priest) on Feb 20, 2003 at 18:48 UTC | |
by hv (Prior) on Feb 20, 2003 at 19:37 UTC | |
|
Re: Parsing arguments
by Pardus (Pilgrim) on Feb 20, 2003 at 20:15 UTC |