Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Odd regex results with g switch

by JamesNC (Chaplain)
on Oct 30, 2005 at 12:18 UTC ( [id://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

Replies are listed 'Best First'.
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
Domain Nodelet?
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?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2024-03-28 22:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found