Phoenix.pm: Regex question

jim mckay jimm at amug.org
Mon Feb 5 17:14:01 CST 2001


anthony's solution is good but doesn't work if you have 2 space seperated
non-quoted strings.. ie   qq(DailyNAV "Class C" nav or "another nav" again);

so..

my $string = qq(DailyNAV "Class C" nav or "another nav" again);

$string =~ s/"(.*?)"/ $x=$1 ;$x =~ s\/\s\/+\/g; $x;/eg ; ###escape spaces
@res=split(/ /,$string);
print join("|", at res)."\n";

If anyone can read this, congratulations! I'm basically escaping it for url (+'s
for all the spaces) kinda sloppy but it would work for html. You could use
anything in place of the spaces. I was trying to figure out how to split it with
"\ " for a space but.. I am a CGI guy so..
 :)

Jim

On 2/5/01 at 12:59 PM, doug.miles at bpxinternet.com (Doug Miles) wrote:

> Josh Hardison at ADS wrote:
> > 
> > Doug,
> >         Take the paren's out of the expression:
> > my @tokens = $string =~ /"[^"]+"|\S+/g;
> > 
> > When you match on one side of the pipe, you get an empty match on the
> > other.  Something like that, anyway.  Kinda unintuitive.
> 
> Yeah, this is the behavior I want, but I don't want the quotes.  I know
> I could s/// them, but I wanted to figure the regex out.  Thanks!
> 
> > 
> > On Fri, 2 Feb 2001, Scott Walters wrote:
> > 
> > >
> > > Doug,
> > >
> > > |DailyNAV|Class C|||nav        <-- output of your version
> > > ||DailyNAV| |Class C|| ||nav   <-- output of same regex used in split() on 
> same string
> > >
> > > Regex can match 0 character things, like with split //, $str;... but
looking
> > > at the regex, nothing in there would match something 0 chracters long, so
> > > I don't know =)
> > >
> > > Hmm. Tried a few other regexes that also "should work" and had no luck.
Only
> > > thing I can think of is work around the mystery/problem:
> > >
> > > my @tokens = grep { $_ ? 1 : 0 } $string  =~ /"([^"]+)"|(\S+)/g;
> > >
> > > -scott
> > >
> > >
> > >
> > > On Fri, 2 Feb 2001, Doug Miles wrote:
> > >
> > > > This is probably something obvious, 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
> > > >
> > > > The first two regexes do what I expect.  When I combine them in the
> > > > third, I get extra "null matches".  Any ideas as to what I'm doing
> > > > wrong?
> > > >
> > > > --
> > > > - Doug
> > > >
> > > > Encrypted with ROT-26 - all attempts to decrypt are illegal under the
> > > > DMCA!
> > > >
> > >
> 
> -- 
> - Doug
> 
> Encrypted with ROT-26 - all attempts to decrypt are illegal under the
> DMCA!
> 



More information about the Phoenix-pm mailing list