Aha! One of the classic mistakes was made on this code:
$myvar =~ /" # First quote
( # Capture text to $1
(?: # Non-backreferencing parentheses
[^?"] # Anything that's not a question mark or quote
| # or
\?[^"] # A question mark not followed by a quote (to a
+llow embedded question marks)
)* # Zero or more
) # End capture
\?"/x; # Followed by a question mark and quote
Try this with
$myvar = q{ abc"def??"ghi?"jkl };
And you'll see that it matches the ghi, not def??.
The problem is that
the "question mark NOT followed by a quote" can sometimes eat up the question
mark that you need to begin your closing delimiter.
The proper way to tackle this is to "inch-along"...
$myvar = q{ abc"def??"ghi?"jkl };
print "matched <$1>" 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
which properly prints:
matched <def?>
I was tackling this kind of thing a lot when people would keep posing the "how
do I match a C comment?" back in the early days Pre-Ilya-RE. I got pretty good
at breaking just about any regex that claimed to match a comment, by undoing
any assumption made.
-- Randal L. Schwartz, Perl hacker
-
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.
|