Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

regex

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.

like image 791
Mona Avatar asked Mar 05 '26 07:03

Mona


1 Answers

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.)

like image 168
ruakh Avatar answered Mar 07 '26 22:03

ruakh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!