Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Regex matches string, i do not know why?!

by tobias_hofer (Friar)
on Apr 26, 2013 at 12:31 UTC ( #1030814=perlquestion: print w/replies, xml ) Need Help??
tobias_hofer has asked for the wisdom of the Perl Monks concerning the following question:


I got following string:
and i do a regex on this in an if statement:
if ( ($__item =~m/(?!Eth.+cfg)Eth/i) || ($__item=~m/(?!abc_Eth.+cfg)abc_Eth/i) )

Its matching.. I do not know why.. It should not match
as in the string there is no "Eth" and no "abc_Eth"..

This makes me going crazy..
Any help is welcome!
Best regards! Tobias

Replies are listed 'Best First'.
Re: Regex matches string, i do not know why?!
by Corion (Pope) on Apr 26, 2013 at 12:38 UTC
    #!perl -wl use strict; my $__item= 'abc_IoExtFscInjS_GetGlobalDatasetActvMethod'; warn $__item; if ( ($__item =~ m/(?!Eth.+cfg)Eth/i) || ($__item =~ m/(?!abc_Eth.+cfg +)abc_Eth/i) ) { print $`; print $&; print $'; print "match"; } else { print "No match"; }; __END__ abc_IoExtFscInjS_GetGlobalDatasetActvMethod at line 6. abc_IoExtFscInjS_GetGlobalDatasetActvM eth od match

    This had me stumped first, but when Perl claims it finds a match, it can also tell you, where :)

Re: Regex matches string, i do not know why?!
by hdb (Monsignor) on Apr 26, 2013 at 12:39 UTC

    In mETHod there is one... matches due to /i

      Thanks a lot!
      I am getting code-blind..

      Thank you for saving my day!

Re: Regex matches string, i do not know why?!
by choroba (Bishop) on Apr 26, 2013 at 12:41 UTC
    Try adding grouping parentheses to see what is matching:
    my $s = 'abc_IoExtFscInjS_GetGlobalDatasetActvMethod'; print "1 $1\n" if $s =~ /((?!Eth.+cfg)Eth)/i; print "2 $1\n" if $s =~ /((?!abc_Eth.+cfg)abc_Eth)/i;


    1 eth

    You are using the /i modifier which makes the matching case insensitive. The string indeed contains "eth" which is not preceded by "eth.+cfg".

    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Regex matches string, i do not know why?!
by dasgar (Priest) on Apr 26, 2013 at 22:29 UTC

    Although others have already helped out, thought I'd toss out another method. You can use DamianConway's Regexp::Debugger module to step through the matching process. For a video demo, you can watch his video presentation from YAPC::NA 2012 at (Note: At the time of the video recording, the module had not yet been released to CPAN.)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1030814]
Approved by Corion
and a soft breeze sighs...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2018-03-18 20:22 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (230 votes). Check out past polls.