Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Global regex giving up too soon

by Wassercrats
on Jan 20, 2004 at 19:50 UTC ( #322685=note: print w/ replies, xml ) Need Help??


in reply to Global regex giving up too soon

This is what confused me--from http://perldoc.com/perl5.8.0/pod/perlretut.html

A failed match or changing the target string resets the position. If you don't want the position reset after failure to match, add the //c, as in /regexp/gc. The current position in the string is associated with the string, not the regexp. This means that different strings have different positions and their respective positions can be set or read independently.
I interpreted the third sentence to mean the opposite of what it meant.

There doesn't seem to be anything on /c or /gc in perlre at all, except for a "see also" reference to perlretut. Perlre is the only Reference Manual page to cover regexes, and I think it should at least mention /c and not simply refer people to a Tutorials page.


Comment on Re: Global regex giving up too soon
Re: Re: Global regex giving up too soon
by ysth (Canon) on Jan 21, 2004 at 12:35 UTC
    I think you are suffering a little confusion. perlre documents everything to do with compiling or matching regexes, including all the applicable flags (i, m, s, x). All the other flags have nothing to do with compiling or matching a regex; instead they affect the operator that is using the regex, and are documented in perlop, which says:
    In scalar context, each execution of m//g finds the next match, returning true if it matches, and false if there is no further match. The position after the last match can be read or set using the pos() function; see perlfunc/pos. A failed match normally resets the search position to the beginning of the string, but you can avoid that by adding the /c modifier (e.g. m//gc). Modifying the target string also resets the search position.
    Note that //c is only documented to work with m//g, and even then only in scalar context (though it actually works even in list context).

    Update: perlre says it better than I did. Almost at the very top:

    For reference on how regular expressions are used in matching operations, plus various examples of the same, see discussions of m//, s///, qr// and ?? in perlop/"Regexp Quote-Like Operators".

    Matching operations can have various modifiers. Modifiers that relate to the interpretation of the regular expression inside are listed below. Modifiers that alter the way a regular expression is used by Perl are detailed in perlop/"Regexp Quote-Like Operators" and perlop/"Gory details of parsing quoted constructs".

      I was looking for /g related stuff. If /c isn't explained in perlre near the /g information, then a referral to /c information should be given. I don't read any document in its entirety when I look this stuff up, especially long technical ones, and I like indexing and references that cater to the lazy. Whoever wrote perlretut had the right idea, though the explanation wasn't idiot-proof enough for me.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (15)
As of 2014-09-02 17:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (25 votes), past polls