Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

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:

Hello

I got following string:
abc_IoExtFscInjS_GetGlobalDatasetActvMethod
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

Comment on Regex matches string, i do not know why?!
Select or Download Code
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 tmp.pl 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 (Prior) 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!
      Tobias

Re: Regex matches string, i do not know why?!
by choroba (Canon) 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;

    Output:

    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 (Curate) 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 http://youtu.be/zcSFIUiMgAs. (Note: At the time of the video recording, the module had not yet been released to CPAN.)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2015-07-28 06:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (252 votes), past polls