Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Re: [Perl 5.14, regex]: Problems with /g, \G and pos()

by wjw (Curate)
on Oct 19, 2013 at 13:41 UTC ( #1058918=note: print w/replies, xml ) Need Help??

in reply to [Perl 5.14, regex]: Problems with /g, \G and pos()

Was curious about this because I am laughably ignorant when it comes to regex's. So I looked Here under the "\G Magic with Perl" section.

Subsequently, I Googled "perl position of last match" which led me to this which led me to try the following:

my $str = " xyz" print "$str\n"; $str =~ /^(\s*)/g or die; print("match1: pos=", pos($str), "\n"); print "$str\n"; print "@-\n"; print "@+\n"; $str =~ /\G(\s*)/g or die; print("match2: pos=", pos($str), "\n"); print "$str\n"; print "@-\n"; print "@+\n"; $str =~ /\G(\s*)/g or warn; print("match3: pos=", pos($str), "\n"); print "$str\n"; print "@-\n"; print "@+\n";
Which outputs
xyz match1: pos=2 xyz 0 0 2 2 match2: pos=2 xyz 2 2 2 2 Warning: something's wrong at /tmp/ line 20. Use of uninitialized value in print at /tmp/ line 21. match3: pos= xyz 2 2 2 2
which I think might help describe what happens...(of course I could be wrong)

When \G goes to the position of the previous match to find the next white space, it is already beyond the last position of white space in the string. The warning is sort of uninformative though... . I gotta wonder if the last position and first position being equal leave the \G wondering where to go next?

Am looking forward to other responses to this by those who know...

(yet another opportunity to display my ignorance! Hot Damn!)

UPDATE: looks like the tool mentioned Regex analysis might be helpful with this Hope that is helpful....
...the majority is always wrong, and always the last to know about it...
Insanity: Doing the same thing over and over again and expecting different results.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1058918]
[Eily]: you could have tried randomly swapping your fingers
[Eily]: enigma style
[choroba]: so it was you who typed the whole phrases?
[Yaerox]: I feel like i'm not seeing the forest for the trees. Can someone give me a hint how to research converting all kinds of files into ansi? If origin file is utf8 convert to ansi, is origin file is ansi then skip file.

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (17)
As of 2017-03-28 13:09 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (331 votes). Check out past polls.