gnieddu has asked for the wisdom of the Perl Monks concerning the following question:
I'm practicing on regular expressions. I've tried using the /g switch as in the following piece of code:
$string = "<name=\"foo\"><anystring 1 /></name><name=\"bar\"><anystrin +g 2 /></name>"; while ($string =~ m/<name=\"(.*)\">(.*)<\/name>/g) { print "$1, $2\n"; }
The idea was to get two lines like:
foo, <anystring 1 />
bar, <anystring 2 />
Instead I get a single line like:
foo"><anystring 1 /></name><name="bar, <anystring 2 />
Can anybody explain me why, and how can I modify the code in order to get what I want?
Thanks
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Can't understand why /g does not work as I expect
by toolic (Bishop) on Mar 19, 2010 at 12:56 UTC | |
by gnieddu (Initiate) on Mar 23, 2010 at 10:01 UTC | |
|
Re: Can't understand why /g does not work as I expect
by ww (Archbishop) on Mar 19, 2010 at 13:18 UTC | |
|
Re: Can't understand why /g does not work as I expect
by Anonymous Monk on Mar 19, 2010 at 13:09 UTC |