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?


In reply to Re^2: Regular expression by pravakta
in thread Regular expression by pravakta

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.