Regex question

tcpeter at tcpeter at
Fri Feb 2 21:35:56 CST 2001

On Fri, 2 Feb 2001, Doug Miles wrote:

> This is probably something obvious, 

I don't think so...

> but I don't have my regex book with
> me, and can't seem to figure it out.  I'm trying to parse space
> delimited information, somewhat like the UNIX command line.  Whitespace
> delimits parameters, but parameters can containg whitespace if
> surrounded by quotes.  Here's the code:
> #!/usr/bin/perl
> my $string = qq(DailyNAV "Class C" nav);
> my @tokens = $string =~ /(\S+)/g;
> my @tokens = $string =~ /"([^"]+)"/g;

> my @tokens = $string =~ /"([^"]+)"|(\S+)/g;
> print join('|', @tokens) . "\n";
> here is the output:
> DailyNAV|"Class|C"|nav
> Class C
> |DailyNAV|Class C|||nav

I've been sorta lurking here a long time reading, but this is the first time I've contributed.  This seems to work.  It's really the same as one of yours, with just a bit of tweaking.


my $string = qq(DailyNAV "Class C" nav);
print join('|', split(/\s?"([^"]+)"\s?/, $string))."\n";

More information about the Phoenix-pm mailing list