Help with /g match option

D

dolpho37

All,

On page 191 of Perl Cookbook you find this code snippet:

$n = " 49 here";
$n =~ s/\G /0/g;
print $n;
00049 here

Under discussion is \G matching the last position by virtue of /g
match option being present. What I'm having a hard time understanding
is why the pattern '\G ' doesn't match the space between the '9' and
'h' of '49 here'. Can someone help me out? Thanks in advance.

Kind regards,
Dolpho37
 
J

Jeff 'japhy' Pinyan

[posted & mailed]

$n = " 49 here";
$n =~ s/\G /0/g;
print $n;
00049 here
Under discussion is \G matching the last position by virtue of /g
match option being present. What I'm having a hard time understanding
is why the pattern '\G ' doesn't match the space between the '9' and
'h' of '49 here'. Can someone help me out? Thanks in advance.

It's because \G HAS to match where /g left off, or else ^ if the regex
hasn't matched yet. So /\G / matches the FIRST space, the SECOND space,
and the THIRD space. But then /\G / can't match, because the character at
the location \G matches is a '4', not a ' '.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top