hi, i'm trying to get all strings not containing <start|sidebar>. the problem now? i can't change logic <=~> to <!=> which would work perfectly. reading perldoc and other sources negativ lookahead should solve this problem as well. this one works perfectly
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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.