The /g modifier is working just fine, but your regex is too greedy (see perlreref). Change .* to .*?
$string = "<name=\"foo\"><anystring 1 /></name><name=\"bar\"><anystrin +g 2 /></name>"; while ($string =~ m/<name=\"(.*?)\">(.*?)<\/name>/g) { print "$1, $2\n"; } __END__ foo, <anystring 1 /> bar, <anystring 2 />
If this is XML, I recommend using a parser instead of regular expressions. It will save you a lot of trouble.

As an aside, you could avoid excessive back-whacking in your string by using a different quote character. For example, switch to single quotes. In my opinion, this makes the code easier to understand and maintain.

$string = '<name="foo"><anystring 1 /></name><name="bar"><anystring 2 +/></name>';

Similarly, in your regex, there is no need to escape ", nor is there a need to escape / if you switch to a different delimiter, such as {}

while ($string =~ m{<name="(.*?)">(.*?)</name>}g)

In reply to Re: Can't understand why /g does not work as I expect by toolic
in thread Can't understand why /g does not work as I expect by gnieddu

Title:
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, details, 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, summary, 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.