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.
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". | [reply] [d/l] [select] |
|
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.
| [reply] |
|
You might invite yourself to write a documentation update proposal for perlretut . You seem to know how perlretut should cater the lazy. Oh wait, you are lazy, hmmm... :-)
Anyway, I read perlretut and since I still did not completely understood it, I also read perlrequick,
Perl Intro regarding Regular Expressions,
PerlFAQ6,
Five Habits for Successful Regular Expressions,
perlop, split and finally perlre.
This monastery has a lot more to offer in addition, to start with just what is listed in the Tutorials section:
String matching and Regular Expressions, Quantifiers in regular expressions, Character Class Abbreviations, Pattern-matching examples, Split and join, Pattern Matching Exercises (and solutions), and more complex tutorials by
chromatic, OM_Zen, Cody Pendant, diotalevi and Elian. Abigail-ii wrote numerous insightful nodes about regular expressions.
I should not forget to mention the books
Mastering Regular Expressions
and
Regular Expression Pocket Reference.
And even though I think I don't understand it completely and also I certainly can't remember it all, I do practice it a bit and one day I might answer questions here about it.
I disagree that a referral to /c should be given near the /g info. Regular expressions are so very useful, that though complicated, they deserve close study, and since a lot of information is available for free in quite a readable and understandable manner, reading it will bring you quite some wisdom.
| [reply] |
|
|