in reply to Re^5: Regular expression
in thread Regular expression
my $test_string= '12345'; print "$1\n" if ($test_string=~ /(2)/g); #Actually printed 2 print "$1\n" while ($test_string=~ /\G(\d)/g);# Printed every thing af +ter 2 i.e 3,4,5
Thanks Anomalous for this explanation. If I understand you correctly then essentially you mean \G is a kind of anchor (like ^ and $) but instead of having a fixed location, its position depends on where last match happened. As in my code above first print statement printed only 2 and then for next print statement pattern match started in the string from location next to 2 so 3,4,5 matched.I hope my understanding is correct so far.
Can you tell me what would be the scope of \G. Supposes in next print statement I use a different string, then also \G would will make pattern match to start from a position where it last matched in string one?
what if second string is smaller than string in first print line and \G has a value greater than second string size?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^7: Regular expression
by AnomalousMonk (Archbishop) on Nov 02, 2017 at 22:06 UTC |