in reply to Re^4: Regular expression
in thread Regular expression
"print "Weights are : $1 Kg\n" while $x=~/\G(\d+)\s*kg\s*/ig; #print -1"
The condition for the first while iteration is FALSE: the while loop does not iterate.
Here's a blow-by-blow description of what's occuring. Bear in mind that character positions use a zero-based index: the first character in the string is at postion 0.
I got all that information by running your code through Regexp::Debugger. I highly recommend this module: not only will you find bugs in your regex, you'll also learn a lot about them (in that respect, it's just as useful for regexes that work as those that don't).
You can fix your current problem by adding '.*?' after the '\G':
$ perle 'my $x = q{1 2 3kg 4 5 6 7 8 9 10Kg 11 12 13 kg 14 15}; say $1 + while $x =~ /\G.*?(\d+)\s*kg\s*/gi' 3 10 13
" I will try reading about it more."
The link I provided before does lead to more links. One in particular, which you should definitely read, is "perlop: Regexp Quote-Like Operators". You'll need to scroll down a fair way: look for the "\G assertion" section.
— Ken
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^6: Regular expression
by pravakta (Novice) on Nov 02, 2017 at 20:17 UTC |