Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

m//g in list context

by grain_of_sand (Initiate)
on Aug 08, 2002 at 21:24 UTC ( #188743=perlquestion: print w/ replies, xml ) Need Help??
grain_of_sand has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to debug my understanding of the matching operator. When invoked with the g modifier in list context, I thought that the return value is a list of all possible matches. If capturing parentheses are used, then the list is the set of all $1, $2, etc captured.

The following bit of Perl:
#!/bin/perl -w $" = '-'; $string = "a:b c:d"; @list = ($string =~ m/(\w):(\w)/g); print "list=@list\n"; $string = "a:b:c"; @list = ($string =~ m/(\w):(\w)/g); print "list=@list\n";
prints out:
list=a-b-c-d list=a-b
I understand the first line of output, but it seems to me that the second line of output should really be:
list=a-b-b-c
This is because the (\w):(\w) can match a:b and also b:c. It appears that the regex engine finds the first match of a:b and then starts the next matching attempt after the b, in which case it cannot find the b:c match. I expected the next matching attempt to start after the a, so that the b:c match would also be found.

I have read the Camel book chapter on this & the perlop man page, but they don't really have a lot to say about this...

grain_of_sand

Comment on m//g in list context
Select or Download Code
Re: m//g in list context
by dws (Chancellor) on Aug 08, 2002 at 21:30 UTC
    When invoked with the g modifier in list context, I thought that the return value is a list of all possible matches.

    "All possible" implies matches that overlap with other matches. This isn't how regexs work. With /g, the search for the next match begins immediately after the prior match.

    In your example, applying the regex   m/(\w):(\w)/g
    to   "a:b:c"
    matches once ("a:b"). The next search begins at ":c", which doesn't match.

Re: m//g in list context
by Zaxo (Archbishop) on Aug 08, 2002 at 21:31 UTC

    After the match of 'a:b', pos points to the following colon. The next attempt to match is on ':c'.

    After Compline,
    Zaxo

•Re: m//g in list context
by merlyn (Sage) on Aug 08, 2002 at 22:17 UTC
    To get what you want, use:
    $" = ", "; $string = "a:b:c"; @list = ($string =~ m/(?=(\w):(\w))/g); print "list=@list\n";
    The lookahead operator will keep restarting where the previous match left off.

    -- Randal L. Schwartz, Perl hacker

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (14)
As of 2015-07-06 15:44 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 (77 votes), past polls