linuxfan has asked for the wisdom of the Perl Monks concerning the following question:
I've not been able to figure this simple regex problem. I need to match strings in one of the two formats below (after reading from a file):
"/moreIters 10"
"/bootMe any text here"
/fewIter
If the string begins with a quote, I need all data enclosed within the quotes, else the entire string that begins with /
To begin with, I wrote the following simplistic regex:
which means match zero or 1 double quote, followed by / followed by any number of characters followed by an optional double quote. However, this doesn't work because .* matches the quotes as well, and I get the following:$str =~ m,"?(/.*)"?,
The above works for strings such as /justThis:my $str = qq( "/extend 100" ); $str =~ m,"?(/.*)"?,; if (defined $1) { print "matched=$1\n"; } $perl regex.pl matched=/extend 100"
How can I refine this regex to get the desired result?my $str = qq(/extendMe ); $str =~ m,"?(/.*)"?,; if (defined $1) { print "matched=$1\n"; } $perl regex.pl matched=/extendMe
Thanks!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: dumb regex question
by Nkuvu (Priest) on Apr 07, 2009 at 00:10 UTC | |
by linuxfan (Beadle) on Apr 07, 2009 at 00:38 UTC | |
by ikegami (Patriarch) on Apr 07, 2009 at 01:15 UTC | |
by Nkuvu (Priest) on Apr 07, 2009 at 01:25 UTC | |
by Nkuvu (Priest) on Apr 07, 2009 at 01:01 UTC | |
by linuxfan (Beadle) on Apr 07, 2009 at 00:24 UTC | |
|
Re: dumb regex question
by ELISHEVA (Prior) on Apr 07, 2009 at 10:08 UTC | |
by linuxfan (Beadle) on Apr 07, 2009 at 16:46 UTC |