Dear Monks and fellow followers,
I am unable to understand why my regex is not working the way I intended. I'm trying to write a regex that recognizes when a parameter value is enclosed in single or double quotes and pulls out the value that lies between the quotes, or when the value is not quoted it simply returns the value. Here is my code:
use strict;
my $regex = qr
/ # start of regex
( # start of capturing alternation
(?<=\') # positive lookbehind to a single quote
( # start of capture buffer 2
.*? # the value between the single quotes
) # end of capture buffer 2
(?>\') # positive lookahead to a single quote
| # or
(?<=\") # positive lookbehind to a double quote
( # start of capture buffer 3
.*? # the value between the double quotes
) # end of capture buffer 3
(?>\") # positive lookahead to a double quote
| # or
( # start of capture buffer 4
.* # any unquoted value
) # end of capture buffer 4
) # end of capturing alternation
/x # end of regex
;
&do_test (\"Now is the time"); # test with unquoted value
&do_test (\"'Now is the time'"); # test with single quoted value
&do_test (\'"Now is the time"'); # test with double quoted value
sub do_test
{
print "\n";
if (${$_[0]} =~ /$regex/)
{
print "\$1 is $1.\n";
print "\$2 is $2.\n";
print "\$3 is $3.\n";
print "\$4 is $4.\n";
}
else
{
print "No match.\n";
}
}
When I run this, I get:
$1 is Now is the time.
$2 is .
$3 is .
$4 is Now is the time.
$1 is 'Now is the time'.
$2 is .
$3 is .
$4 is 'Now is the time'.
$1 is "Now is the time".
$2 is .
$3 is .
$4 is "Now is the time".
Why are the first two alternatives not capturing quoted test strings?
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.