Mona
Mona

Reputation: 788

Why regular expression doesn't work with Global identification in Perl?

It is very weird, and I don't have any idea what is the issue!

I have a very big string (length=648745), and I don't know if its length can make this issue, but I'm trying to find some parameters inside it, and push them to an array, like this:

push(@items_ids, [$2, $3]) while ($all_items_list =~ /itemID&(id|num)=([\d]*)\">\#([\d]*)/g);

It doesn't work, it returns an empty array at the end. I thought may be my RegEx is not right, but when I run this code:

while ($all_items_list =~ /itemID&(id|num)=([\d]*)\">\#([\d]*)/){
    print "\nItemID=$2 Identity=$3\n";die;
}

it finds the first occurrence, when I put "g" at the end of ReEx it can't find it any more...

I know I'm missing something here, Please help me, this is not a hard part of my script and I'm stuck, :( ...

Thanks in advance for your help.

Upvotes: 3

Views: 168

Answers (1)

ruakh
ruakh

Reputation: 183300

In scalar context, m/.../g starts looking after where a previous successful m/.../g left off. I would suggest resetting the search-position right before the loop:

pos($all_items_list) = undef;
push(@items_ids, [$2, $3]) while ($all_items_list =~ /itemID&(id|num)=([\d]*)\">\#([\d]*)/g);

and seeing if that helps. (See http://perldoc.perl.org/functions/pos.html.)

Upvotes: 4

Related Questions