Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

comment on

( [id://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

m#<code>((?:(?!foo).)*)</code>#
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

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



  • 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.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2024-04-20 13:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found