Hi bbs2web,

Think of positive/negative look-ahead/look-behind regular expression patterns like this:

Positive Lookahead:
(PATTERN_OF_INTEREST)(?=FOLLOWING_PATTERN)
Capture PATTERN_OF_INTEREST when it precedes the FOLLOWING_PATTERN

e.g.    'this(?=\ that)' # match 'this' when followed by ' that')

Negative Lookahead:
(PATTERN_OF_INTEREST)(?!FOLLOWING_PATTERN)
Capture PATTERN_OF_INTEREST when it DOES NOT precede the FOLLOWING_PATTERN

e.g.    'this(?!\ that)' # match 'this' when NOT followed by ' that'

Positive Look-behind:
(?<=PRECEDING_PATTERN)(PATTERN_OF_INTEREST)
Capture PATTERN_OF_INTEREST when it follows the PRECEDING_PATTERN

e.g.    '(?<=this\ )that' # match 'that' when preceded by 'this '

Negative Look-behind:
(?<!PRECEDING_PATTERN)(PATTERN_OF_INTEREST)
Capture PATTERN_OF_INTEREST when it DOES NOT follow the PRECEDING_PATTERN

e.g.    '(?<!this\ )that' # match 'that' when NOT preceded by 'this '

Both the PRECEDING_PATTERN and the FOLLOWING_PATTERN are matched as the 'look-around' patterns, but neither are captured to a variable, only what is matched as part of your (PATTERN_OF_INTEREST) capture group (i.e. if you use capturing parenthesis).

Edit: replaced 'followed' with 'preceded' as correctly pointed out by AnomalousMonk. Thanks for spotting that :) It was late...

In reply to Re: Remove all lines, except those starting with pattern by perlygapes
in thread Remove all lines, except those starting with pattern by bbs2web

Title:
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, details, 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, summary, 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.