Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
++moritz 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:
#!/usr/bin/perl -w use strict; LINE: while ( <DATA> ) { MATCH: while ( m#<code>(.*?)</code>#gi ) { print "$1\n" if ( $1 !~ /foo/ ); } } __DATA__ 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></code> <code>is foo rejected</code> <code>foo</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.

In reply to Re: Look-Arounds in Regexes are Hard by papidave
in thread Look-Arounds in Regexes are Hard by moritz

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • 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, 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
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others perusing the Monastery: (4)
    As of 2020-09-28 12:35 GMT
    Find Nodes?
      Voting Booth?
      If at first I donít succeed, I Ö

      Results (143 votes). Check out past polls.