Monks,
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:
$str =~ m,"?(/.*)"?,
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:
my $str = qq( "/extend 100" );
$str =~ m,"?(/.*)"?,;
if (defined $1) {
print "matched=$1\n";
}
$perl regex.pl
matched=/extend 100"
The above works for strings such as /justThis:
my $str = qq(/extendMe );
$str =~ m,"?(/.*)"?,;
if (defined $1) {
print "matched=$1\n";
}
$perl regex.pl
matched=/extendMe
How can I refine this regex to get the desired result?
Thanks!
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.