in reply to Re^4: Regular expression
in thread Regular expression
Ohh my bad. I corrected G with \G but still no chnage ... prints nothing. Still not sure what difference it was supposed to make.
The \G anchor matches at the point (the exact character offset in the string) at which matching stopped in the last /g global match iteration. But on the first /g iteration, where is that point? On the first /g iteration, \G matches the same as \A (the \Absolute-start-of-string anchor).c:\@Work\Perl\monks>perl -wMstrict -le "my $x = '1 2 3kg 4 5 6 7 8 9 10Kg 11 12 13 kg 14 15'; print qq{string \$x: '$x'}; ;; printf qq{captured '$1' } while $x =~ /\G(\d+)\s*kg\s*/ig; print '----------'; " string $x: '1 2 3kg 4 5 6 7 8 9 10Kg 11 12 13 kg 14 15' ----------
So what /\G(\d+)\s*kg\s*/ig says is:
But your '1 2 3kg 4 5 6 7 8 9 10Kg 11 12 13 kg 14 15' string begins with some digits, some whitespace, and then some more digits, not the required 'kg' literals: the match immediately fails. There is a '3kg' subsequence further on that could satisfy part of the overall match, but matching has already failed due to the \G assertion.
Give a man a fish: <%-{-{-{-<
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^6: Regular expression
by pravakta (Novice) on Nov 02, 2017 at 20:02 UTC | |
by AnomalousMonk (Archbishop) on Nov 02, 2017 at 22:06 UTC |