note
Ovid
Okay, the title is kind of a joke. It's just a good-natured tweak at [merlyn] for the brouhaha over his [WARNING t0mas wrote BAD CODE] node that generated so much flak. No offense intended :)
<P>
[merlyn|merlyn's] code was bugging me, but I couldn't quite put my finger on it. My problem was that the dot metacharacter is so indiscriminating that it will match anything. However, I simply assumed that if [merlyn] posted the code, it must work. His code is great if you're checking for C-style comments that begin and end in something like <TT>/* comment here */</TT> or <TT>"? comment here ?"</TT>. But if you read my post, that's not what we were checking for:
<UL>
What happens if you were trying to extract questions in quotes without the trailing question mark?
</UL>
I mentioned embedded question marks (my idea was that we might have more than one question in a quote), but I never mentioned embedded quotes. I just wanted one set of quotes and my original post bears that out. Here's [merlyn|merlyn's] code and my correction:
<CODE>
#!/usr/bin/perl -w
$myvar = q{ abc"def"g"hi?"jkl };
# This regex is from merlyn
print "matched <$1>\n" if
$myvar =~ /" # First quote
( # Capture text to $1
(?: # Non-backreferencing parentheses
(?!\?") # not question quote?
. # ok to inch along
)* # Zero or more
) # End capture
\?"/sx; # Followed by a question mark and quote
# This regex is from Ovid
print "matched <$1>\n" if
$myvar =~ /" # First quote
( # Capture text to $1
(?: # Non-backreferencing parentheses
[^?"] # Not a question mark or parentheses
| # or
\?(?!") # A question mark not followed by a quote
)* # Zero or more
) # End Capture
\?"/sx; # Followed by a question mark and quote
</CODE>
The first regex will print <TT>matched <def"g"hi></TT>. The second will print <TT>matched <hi></TT>.
<P>
No disrespect is intended towards Randal as he was right in pointing out that my first regex was broken.
<P>
Cheers,<BR>
[Ovid]
24640
24661