in reply to Re: Regular expression
in thread Regular expression
Hi Ken,
A big thanks to you for your detailed analysis and patience to explain things. You guessed it right I am relatively new to ‘serious perl’ learning and have been experimenting with the language.
Now coming back o the problem. I understood points you made. You are right that primary motive of my code snippet was to understand the modifiers \g and \G.
to better apprciate the effect of \g ad \G I wrote a sample code as follows-
my $x= '1 2 3kg 4 5 6 7 8 9 10Kg 11 12 13 kg 14 15'; chomp $x; print "Values in variabe x are : \n$x\n"; $x =~ m/(?<weight>\d+)\s*/g; my $weight = $1; print "Matched pattern is : $+{weight}\n"; print "Values in variabe x are : \n$x\n"; #print "Weights are : $1 Kg\n" while $x=~/G(\d+)\s*kg\s*/ig; #print - +1 #print "Weights are : $1 Kg\n" while $x=~/(\d+)\s*kg\s*/ig; #print -2 print "Weights are : $1 Kg\n" while $x=~/(\d+)\s*kg\s*/i; #print -3
My expected result is<\p>
Weights are 3kg
Weights are 10kg
Weights are 13kg
I have used three print statements #print-1/2/3. My observations-
print-1-> I was expecting this to be the right statement for my output requirement. But enabling this doesn't seem to have any thing matching. No print.
print-2-> this one does the job. As I understand its kind of global match and with every iteration of the loop it start looking in the string from a point where it matched last.
print-3-> goes in a infinite loop whihc I understand due the fact that every iteration of the loop start looking from the start of the string and it always find 3kg there. So it only print 3kg in infinite loop.
Please add some insight on what is the importance of \G and what are some practice usage scenario of \G?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Regular expression
by kcott (Archbishop) on Oct 31, 2017 at 22:37 UTC | |
by pravakta (Novice) on Oct 31, 2017 at 23:15 UTC | |
by kcott (Archbishop) on Nov 01, 2017 at 03:08 UTC | |
by pravakta (Novice) on Nov 02, 2017 at 20:17 UTC | |
by AnomalousMonk (Archbishop) on Nov 01, 2017 at 01:20 UTC | |
by pravakta (Novice) on Nov 02, 2017 at 20:02 UTC | |
by AnomalousMonk (Archbishop) on Nov 02, 2017 at 22:06 UTC |