mmontemuro has asked for the wisdom of the Perl Monks concerning the following question:

I need help with a regular expression that contains a look ahead. I have the following code that ignores my “not expression”. If I change the ! to an =, then it only matches the lines that contain TEXT. What I’m looking for are all lines that begin with only L1 and L3, but don’t contain the word TEXT. Is this possible using 1 regular expression? I thought that is what the look ahead function does. Thanks for any assistance.
Code Snippet: #!/usr/bin/perl -w use strict; while (<DATA>) { chomp; if (/^(L[13].*(?:(?!TEXT).*).*)/) {print "$_\n";} } __DATA__ L1 04:10:07.915 LOG: Want this Line1 L3 04:10:07.915 LOG: Want this Line2 L1 04:10:08.024 LOG: TEXT. Do not want this Line3 L3 06:37:58.163 LOG: TEXT. Do not want this Line4 L3 07:02:36.921 LOG: Want this Line5 L4 08:02:30.910 LOG: Do not want this Line6 L5 08:02:36.943 LOG: Do not want this Line7 L6 09:02:38.811 LOG: Do not want this Line8 __END__ Expected: L1 04:10:07.915 LOG: Want this Line1 L3 04:10:07.915 LOG: Want this Line2 L3 07:02:36.921 LOG: Want this Line5 But get: L1 04:10:07.915 LOG: Want this Line1 L3 04:10:07.915 LOG: Want this Line2 L1 04:10:08.024 LOG: TEXT. Do not want this Line3 L3 06:37:58.163 LOG: TEXT. Do not want this Line4 L3 07:02:36.921 LOG: Want this Line5

Replies are listed 'Best First'.
Re: RegEx Question - I want a line that contains this, but not this
by johngg (Canon) on Mar 30, 2010 at 13:17 UTC

    I'm not sure why you are chomping unless you are doing more with the data than you have posted. Also, you are capturing then not using it. Once you have your "L1" or "L3" put the "zero or more of anything" inside the negative look-ahead and make it non-greedy.

    use strict; use warnings; while ( <DATA> ) { print if m{^L[13](?!.*?TEXT)}; } __DATA__ L1 04:10:07.915 LOG: Want this Line1 L3 04:10:07.915 LOG: Want this Line2 L1 04:10:08.024 LOG: TEXT. Do not want this Line3 L3 06:37:58.163 LOG: TEXT. Do not want this Line4 L3 07:02:36.921 LOG: Want this Line5 L4 08:02:30.910 LOG: Do not want this Line6 L5 08:02:36.943 LOG: Do not want this Line7 L6 09:02:38.811 LOG: Do not want this Line8

    Produces

    L1 04:10:07.915 LOG: Want this Line1 L3 04:10:07.915 LOG: Want this Line2 L3 07:02:36.921 LOG: Want this Line5

    I hope this is helpful.

    Cheers,

    JohnGG

Re: RegEx Question - I want a line that contains this, but not this
by rubasov (Friar) on Mar 30, 2010 at 18:29 UTC
    johngg's previous answer works perfectly, I only would like to show you a more general technic to express "I want my regex to match this, but do not match that". The key is to use a positive and a negative look-ahead together:
    #! /usr/bin/perl use strict; use warnings; my $re_yummy = qr/^L[13]/; my $re_yucky = qr/TEXT/; while (<DATA>) { print if / \A (?= .* $re_yummy ) (?! .* $re_yucky ) /x; } __DATA__ L1 04:10:07.915 LOG: Want this Line1 L3 04:10:07.915 LOG: Want this Line2 L1 04:10:08.024 LOG: TEXT. Do not want this Line3 L3 06:37:58.163 LOG: TEXT. Do not want this Line4 L3 07:02:36.921 LOG: Want this Line5 L4 08:02:30.910 LOG: Do not want this Line6 L5 08:02:36.943 LOG: Do not want this Line7 L6 09:02:38.811 LOG: Do not want this Line8