for caring about beginners.
I don't consider myself to be a newby any more, but even so, look-wherever expressions make me a bit crosseyed, and it got worse when I realized that the original question implied a fixed start and end to the pattern. When that happens, a line like
Functions <code>abc()</code> and <code>foo()</code>
should match the first code block, but not the second.
If we want to use lookahead to match <code> blocks that don't contain foo, we might use something like
but you'd be wise to insert a ton of comments to clarify things*. OTOH, I find it far more readable to use something like the following:
LINE: while ( <DATA> )
MATCH: while ( m#<code>(.*?)</code>#gi )
print "$1\n" if ( $1 !~ /foo/ );
This has no match at all, and is skipped.
This has foo, but is skipped with no code block.
<code>This is acceptable</code>
so is <code></code> an embedded block, and the next.
<code>is foo rejected</code>
we will skip <code> with no terminator.
and, we will also skip <code> with foo, but no terminator.
foo that preceeds <code> just </code> simple text is accepted.
here is <code> another </code> example.
but <code> Perl </code> will accept foo outside the block.
The foo can be in advance, as every <code> hacker </code> knows.
Doubled matches like <code></code> foo <code></code> are evil.
This should <code> skip the second loop</code><code> foo </code>.
This <code> foo </code> should <code> skip the first loop </code>.
This gives us the desired multiple-block behaviors, while providing us with the non-foo data (in $1) for each instance, so we can do something more complicated with it if desired. In addition, the nested loop was surprisingly about 10% faster than the look-ahead code when I compared it over 100,000 iterations using Perl v5.10.0.
*Unless you're in the UK; in that case, use a tonne of comments instead. :)
Note: lost a slash in code cut/paste business - repaired.
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:
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
| & || & |
| < || < |
| > || > |
| [ || [ |
| ] || ] ||