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

Sorry saw the output
All If i have a string like

my $string = "58=Invalid Contract;59=3;45=Test;23=Test12
my $string2 = "58=Invalid Contract;45=Test

And i want to be able to search for the string that has
(58=Invalid) and does not have (!59=3)
So something like
my $res =~ /(58=Invalid)(!59=3)/

Replies are listed 'Best First'.
Re: Match and not match
by blazar (Canon) on May 26, 2006 at 12:57 UTC

    You know, sometimes two regexen are better than one:

    /$this/ && !/$that/

    may be the best solution, especially if $this and $that that are not necessarily in order. Otherwise a quick look into perldoc perlre would reveal you that probably what you want is (?!$that).

Re: Match and not match
by Fletch (Bishop) on May 26, 2006 at 12:59 UTC

    You said what you want, just state it in perl:

    if( $res =~ /58=Invalid/ and not $res =~ /59=3/ ) { grobble_error( $res ); } else { print "58=Invalid and 59=3\n"; }
Re: Match and not match
by GrandFather (Saint) on May 26, 2006 at 13:02 UTC

    If I understand what you want then the followin regex code using a negative lookahead assertion is one way to do the trick:

    use strict; use warnings; while (<DATA>) { print "Match $_" if /58=Invalid(?:(?!59=3).)*$/; } __DATA__ 58=Invalid Contract;59=3;45=Test;23=Test12 58=Invalid Contract;45=Test

    Prints:

    Match 58=Invalid Contract;45=Test

    DWIM is Perl's answer to Gödel