Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

regular expression inverse match NOT match?

by virtuemart2 (Novice)
on Sep 25, 2013 at 01:30 UTC ( #1055584=perlquestion: print w/ replies, xml ) Need Help??
virtuemart2 has asked for the wisdom of the Perl Monks concerning the following question:

I would like to ask further, if I would like to programming like this: NOT match the keyword

if($string !=~ m/keyword/) {print "howto NOT inverse match?"};

what should be the correct gramma syntax?

Comment on regular expression inverse match NOT match?
Download Code
Re: regular expression inverse match NOT match?
by Athanasius (Monsignor) on Sep 25, 2013 at 02:02 UTC
Re: regular expression inverse match NOT match?
by ww (Bishop) on Sep 25, 2013 at 02:39 UTC
    There's really not much wrong with your sample code, if I understand your intent correctly:
    perl -E "my $string = 'abc efg hijk lmnop'; if($string !=~ m/defg/) {s +ay $string;}" abc efg hijk lmnop

    If it does the job without side-effects, it's probably correct!

    But is there some reason you couldn't have answered this yourself... or at least satisfied yourself that your code does or does not work?

    Update on my oops: TY, Athanasius who pointed out my typo and, implicitly, that -E does not invoke warnings, whose use would have also pointed out my mistook. With a

    use warnings;<c> added to the one-liner this is evident:</p> <c>!=~ should be !~ at -e line 1. Use of uninitialized value $_ in pattern match (m//) at -e line 1. Argument "abc efg hijk lmnop" isn't numeric in numeric ne (!=) at -e l +ine 1.

    With the correction, and a more generous example of strings, however, IJW:

    C:\>perl -E "use warnings; my @strings = ('12345 defg', 'abc efg hijk +lmnop', 'xyz defg abc'); for my $string(@strings) { if($string !~ m/d +efg/) {say $string;}}" abc efg hijk lmnop

    Which just goes to say, OP had the "not much wrong" idea.

Re: regular expression inverse match NOT match?
by virtuemart2 (Novice) on Sep 25, 2013 at 06:00 UTC
    "But is there some reason you couldn't have answered this yourself... or at least satisfied yourself that your code does or does not work?"
    Firstly, thx for reply. It not work because just something wrong with my logic. Now I know the correct syntax, should be !~ but not !=~
    furthermore want to ask, if I got keyword
    AB CDE #don't know number of space between AB CDE #5 space =~ /AB.....CDE/ AB CDE #7 space =~ /AB.......CDE/

    If I do NOT know number of space between AB and CDE
    what symbol should I use in this case? thank you!

      You could use either of those:

      /AB +CDE/; # one or more spaces /AB\s+CDE/; # one or more spacing characters (incl. spaces, tabs, end +of line, etc.)

      If it's just spaces, then you can use

      /AB\s*CDE/

      But if they can be any characters except the sequence CDE, you can use

      /AB(?:(?!CDE).)*CDE/s

      (?:(?!PAT).)* is to PAT as [^CHAR]* is to CHAR.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1055584]
Approved by Athanasius
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (7)
As of 2014-07-28 07:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (193 votes), past polls