Ohh my bad. I corrected G with \G but still no chnage ... prints nothing. Still not sure what difference it was supposed to make.
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'
----------
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).
So what /\G(\d+)\s*kg\s*/ig says is:
-
\G From the string offset at which the previous match stopped (or from the start of the string if it's the first match);
-
(\d+) Match and capture one or more decimal digits;
-
\s* Then match zero or more whitespace characters;
-
kg Then match the literal characters 'kg' case-insensitively (due to the /i flag);
-
\s* Then match zero or more whitespace characters (this can't fail);
-
And this match iteration is finished.
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: <%-{-{-{-<
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.