Beefy Boxes and Bandwidth Generously Provided by pair Networks Frank
Think about Loose Coupling
 
PerlMonks  

Odd regex results with g switch

by JamesNC (Chaplain)
on Oct 30, 2005 at 12:18 UTC ( #503986=perlquestion: print w/ replies, xml ) Need Help??
JamesNC has asked for the wisdom of the Perl Monks concerning the following question:

Here is my environment: Win32 + AS perl v5.8.7 build 813
Example code:
my $flip = 'vh'; if( $flip =~/v|y/i ){ print "i only -> flip v|y\n"; } if( $flip =~/h|x/i ){ print "i only -> flip h|x\n"; } if( $flip =~/v|y/ig ){ print "ig -> flip v|y\n"; } if( $flip =~/h|x/ig ){ print "ig -> flip h|x\n"; } if( $flip =~/v|y/g ){ print "g only -> flip v|y\n"; } if( $flip =~/h|x/g ){ print "g only -> flip h|x\n"; } #change $flip print "----------------------\n\$flip is now $flip\n------------------ +----\n"; $flip = 'hv'; if( $flip =~/v|y/i ){ print "i only -> flip v|y\n"; } if( $flip =~/h|x/i ){ print "i only -> flip h|x\n"; } if( $flip =~/v|y/ig ){ print "ig -> flip v|y\n"; } if( $flip =~/h|x/ig ){ print "ig -> flip h|x\n"; } if( $flip =~/v|y/g ){ print "g only -> flip v|y\n"; } if( $flip =~/h|x/g ){ print "g only -> flip h|x\n"; }
Output with comments:
i only -> flip v|y (expected this) i only -> flip h|x (expected this) ig -> flip v|y (expected this) ig -> flip h|x (expected this) g only -> flip h|x (expected this) <--- didn't match v ---------------------- $flip is now vh ---------------------- i only -> flip v|y (expected this) i only -> flip h|x (expected this) ig -> flip v|y (expected this) <--- didn't match h g only -> flip v|y (expected this) <---- didn't match h

I was expecting all of these cases to work. But I get odd behavior when I use the 'g' switch in my regex when I switch the string around. Is this a bug? or can someone fill an obvious gap in my understanding of using the g switch in a regex? I didn't know that separate if statements could do this.
Thanks,
JamesNC

Comment on Odd regex results with g switch
Select or Download Code
Re: Odd regex results with g switch
by merlyn (Sage) on Oct 30, 2005 at 12:37 UTC
      thank you

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (8)
As of 2014-04-18 04:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (461 votes), past polls