Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re^2: Re-use of a global match

by gube (Parson)
on Aug 01, 2007 at 10:23 UTC ( #630009=note: print w/ replies, xml ) Need Help??


in reply to Re: Re-use of a global match
in thread Re-use of a global match

Thanks for the above replies..

Hi shmem

I added modifier /c also in the match. Please check the below code. But, still the pos() for regexp is not reset. Could you please help me how to reset the position for regexp variable using modifier
#!/usr/local/bin/perl use strict; use warnings; my $regexp = 'Perl Monks'; print "\nPos...", pos($regexp); if($regexp =~ m/^Perl Monks/gci) { print "\nFound.."; } else { print "\nNot Found.."; } print "\nPos..1.", pos($regexp); if($regexp =~ m/^Perl Monks/gi) { print "\nFound.1."; } else { print "\nNot Found.1."; }


Comment on Re^2: Re-use of a global match
Download Code
Re^3: Re-use of a global match
by shmem (Canon) on Aug 01, 2007 at 10:43 UTC
    I added modifier /c also in the match. Please check the below code. But, still the pos() for regexp is not reset.

    which is the expected behaviour, since (perlop again):

    Options are:
    c  Do not reset search position on a failed match when /g is in effect.
    g  Match globally, i.e., find all occurrences.

    The pos() is retained in all cases until the next match is attempted, so you have to remove the /g modifier on the second match, as casiano correctly noted, which will reset pos() just before the regep engine tries to match the second time.

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://630009]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2014-12-28 06:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (179 votes), past polls