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

Re: What does 'global' (/g) do in a regexp in this particular case?

by LanX (Canon)
on Apr 09, 2013 at 13:37 UTC ( #1027738=note: print w/ replies, xml ) Need Help??


in reply to What does 'global' (/g) do in a regexp in this particular case?

/g in scalar context iterates over all matches and keeps track of the position, see also pos

Without /g the match restarts from the beginning, i.e. the string is stateless.

DB<116> $a="a a" => "a a" DB<117> scalar ($a =~ /a/g) => 1 DB<118> scalar ($a =~ /a/g) => 1 DB<119> scalar ($a =~ /a/g) => "" # exhausted DB<120> $a="a a" => "a a" DB<121> scalar ($a =~ /a/) => 1 DB<122> scalar ($a =~ /a/) => 1 DB<123> scalar ($a =~ /a/) => 1 # ... till infinity

Cheers Rolf

( addicted to the Perl Programming Language)


Comment on Re: What does 'global' (/g) do in a regexp in this particular case?
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2014-07-23 04:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (133 votes), past polls