in reply to Re^3: Regular expression
in thread Regular expression
Ohh my bad. I corrected G with \G but still no chnage
print "Weights are : $1 Kg\n" while $x=~/\G(\d+)\s*kg\s*/ig; #print -1
prints nothing. Still not sure what difference it was supposed to make. NO issues if you don't have much experience to share with this. I will try reading about it more.
Thanks for your help.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: Regular expression
by kcott (Archbishop) on Nov 01, 2017 at 03:08 UTC | |
"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':
" 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 | [reply] [d/l] [select] |
by pravakta (Novice) on Nov 02, 2017 at 20:17 UTC | |
Ahhh I understand it. Anomalous also replied on the same line. and I have a question whihc I put in my reply to him Question is about scope of \G. How long \G hold its value? Till next unsuccessful match? | [reply] |
|
Re^5: Regular expression
by AnomalousMonk (Archbishop) on Nov 01, 2017 at 01:20 UTC | |
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). 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: <%-{-{-{-< | [reply] [d/l] [select] |
by pravakta (Novice) on Nov 02, 2017 at 20:02 UTC | |
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. | [reply] [d/l] |
by AnomalousMonk (Archbishop) on Nov 02, 2017 at 22:06 UTC | |
... 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? Each individual string has an independent "position of end of last successful match" attribute that is returned by the pos built-in. The \G regex operator (enabled by the /g modifier) accesses this attribute of a string being matched to assert that matching in that string is continuing where previous matching in that string by any m//g match left off.
What would have happened if the second match against the $s1 string had been /\Gfoe/g instead? Or /\Gbar/g instead? Try it and see! (See also the documentation concerning the effect of the /c modifier in conjunction with /g in a m//gc match.) (Incidentally, what if $test_string in your example code was '12xxx345' and the match was /(\d)/g (no \G) instead? What if it was /\G(\d)/g as originally?) Give a man a fish: <%-{-{-{-< | [reply] [d/l] [select] |