So building something like this in 1 regex: "zzz <> ttt> ab" - matches "z ab <tt> xx" - matches "zzz ab <>ab> tt" - doesn't match "ab <a x> <ab>" - doesn't match That took me a while to work out but it can be done:
#!/usr/local/bin/perl use strict; use warnings; my @data; $data[0] = "zzz <> ttt> ab"; $data[1] = "z ab <tt> xx"; $data[2] = "zzz ab <>ab> tt"; $data[3] = "ab <a x> <ab>"; foreach my $line ( @data ) { if ( $line !~ m#(?(?=<.*?ab.*?>)((?<!<).*?ab.*?(?!>))|0)# ) { print $line . "\n"; } } To explain it a little: (? #if (?=<.*?ab.*?>) #matches then match ((.*?>)((?<!<).*?ab.*?(?!>)) #else there is no match |0) This will match the two lines you do not want so we use != to match the other two. I am sure you could also put this into a positive formusing =~ but I couldn't be arsed with my manager looking over my sholder wondering why his report isn't finished :-) Regards, Rob On Mon, Jul 21, 2008 at 12:36 PM, Octavian Rasnita <[EMAIL PROTECTED]> wrote: > From: "Rob Coops" <[EMAIL PROTECTED]> > > You could simply say this: > > if ( ! m/ab/ ) { > > #make the world go round > > } > > Or assuming you are looping thru a list you could say something like: > > foreach my $item ( @list ) { > > next if ( $item =~ m/ab/ ); > > #make the world go round > > } > > I also had this problem some time ago but I couldn't find a solution for > it. > I wanted to test something like: > > Match only if the string contains somewhere a "ab" in it but it doesn't > contain "ab" between < and >. > > For example: > > "zzz <> ttt> ab" - matches > "z ab <tt> xx" - matches > "zzz ab <>ab> tt" - doesn't match > "ab <a x> <ab>" - doesn't match > > I've done it by using 2 regular expressions, but I was searching for a > solution for doing using only a single regular expression. > Is it possible to test the existence of a string and the non-existence of > another string using a single regular expression? > > I've tried using (?=...) and (?!...) but these 2 expressions can be used > only if I know where in the string I want a certain string to appear or not > to appear. > > Octavian > >