Keep It Simple, Stupid | |
PerlMonks |
Re: When exactly do Perl regex's require a full match on a string?by gone2015 (Deacon) |
on Feb 08, 2009 at 14:56 UTC ( [id://742260]=note: print w/replies, xml ) | Need Help?? |
why do the regexes /^a$/ and /^a$\n/ match "a\n\n" in only the multi-line mode (they match "a\n" in all three modes)? I think the key here is that, absent /m: $ matches end-of-string or just before a newline at end-of-string. So, /^a$/ matches "a" and "a\n", but not "a\n\n" or "a\nq" -- the latter two because there is something after the "\n" that the $ matches at. Now: /^a$\n/ is a bit odd. It matches "a\n", which I think we can read as: (a) the $ successfully matching in front of the \n, and then the \n matching the \n. It does not match "a" -- because while the $ matches, the \n does not. It also does not match "a\n\n" -- because the $ does not match. [m/.+$\n/ can be read as requiring a not-empty string terminated by exactly one \n.] Looking at /^a$/m, now $ will match end-of-string or just before a newline anywhere in the string. So now it matches "a\n\n" because $ matches before the first \n (under /m it doesn't matter that it's not at end of string), then the \n matches the first \n in the string. And /^a$\n/m, matches "a\n\n" because $ matches before the first \n, then the \n matches the first \n in the string. In passing, I note that Perl accepts m'^a$q' which can never match... Update: with thanks to AnomalousMonk for pointing out my soggy thinking, below -- of course, when $ matches a \n it matches before it. So only m/$\n/ can hope to match ! (I knew that, dammit.)
In Section
Seekers of Perl Wisdom
|
|