Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
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:
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...


In reply to m//g in list context by grain_of_sand

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others imbibing at the Monastery: (7)
    As of 2020-08-07 09:59 GMT
    Find Nodes?
      Voting Booth?
      Which rocket would you take to Mars?

      Results (44 votes). Check out past polls.