use strict; use warnings; my @vars = ('quit','quirly cat', 'cat queue','granit'); print ("any string 'qu' followed by 'it'\n"); foreach (@vars) { if ($_ =~ /qu(?=it)/) { print ("found <$_>\n"); } else { print ("no match for <$_>\n"); } } print ("\nany string 'qu' not followed by 'it'\n"); foreach (@vars) { if ($_ =~ /qu(?!it)/) { print ("found <$_>\n"); } else { print ("no match for <$_>\n"); } }
this one alreay show up some problems
use strict; use warnings; my @vars = ('and gugus for','gugus and', 'gurit for','granit'); print ("any string 'gu' followed by 'gu'\n"); foreach (@vars) { if ($_ =~ /gu(?=gu)/) { print ("found <$_>\n"); } else { print ("no match for <$_>\n"); } } print ("\nany string 'gu' not followed by 'gu'\n"); foreach (@vars) { if ($_ =~ /gu(?!gu)/) { print ("found <$_>\n"); } else { print ("no match for <$_>\n"); } }
question: why does <gugus> show found, when <gu> followed by <gu> should be suppressed? what did i not understand correctly?
finally my real problem:
use strict; use warnings; my @test = ("ananas", "de:mindset:rule1", "en:mindset:rule1", "wiki:we +lcome", " de:sidebar", "sidebar", "en:sidebar", "start", "de:start", "en:start") +; my ($cnt, $result); print "start first loop <var !=> \n"; $cnt=0; foreach (@test) { if ($_ !~ /(start|sidebar)/) { print " >$_<\n"; $cnt++; } } $result = ($cnt eq 4? ">> correct result" : ">> wrong result"); print "end first loop <$cnt> is $result\n\nstart second loop <negative + lookahea d>\n"; $cnt=0; foreach (@test) { if ($_ =~ /(?!start|sidebar)/) { print " >$_<\n"; $cnt++; } } $result = ($cnt eq 4? ">> correct result" : ">> wrong result"); print "end second loop <$cnt> is $result\n"; print ">>pgm ended\n";
question: how is the correct notation for "find any string without start|sidebar" using negative lookahead?
thank you and
In reply to regex negativ lookahead by SwissGeorge
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |