SwissGeorge has asked for the wisdom of the Perl Monks concerning the following question:
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: regex negativ lookahead
by tybalt89 (Monsignor) on Nov 14, 2017 at 07:31 UTC | |
|
Re: regex negativ lookahead
by haukex (Archbishop) on Nov 14, 2017 at 10:57 UTC | |
by SwissGeorge (Initiate) on Nov 14, 2017 at 14:56 UTC | |
by haukex (Archbishop) on Nov 14, 2017 at 15:43 UTC | |
|
Re: regex negative lookahead
by hippo (Archbishop) on Nov 14, 2017 at 09:30 UTC |