Beefy Boxes and Bandwidth Generously Provided by pair Networks
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??


in reply to When exactly do Perl regex's require a full match on a string?

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... unless /m is somehow implied (eg Regexp::Autoflags ?) Perhaps it's just too hard to spot the degenerate case ?

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.)

Replies are listed 'Best First'.
Re^2: When exactly do Perl regex's require a full match on a string?
by AnomalousMonk (Archbishop) on Feb 09, 2009 at 05:53 UTC
    [...] Perl accepts  m'^a$q' which can never match... unless  /m is somehow implied [...]
    With or without the /m modifier, it can never match against any string whatsoever because as the regex is defined,  $ is required to match before something other than an end-of-string or newline:  'q' follows it in the regex.

    If the regex is defined with a newline to follow the  $ metacharacter, if the  /m modifier is used and if the interpolation-suppressing  ' (single-quote) character is used as the regex delimiter, then a match is possible against a string with an embedded newline:

    >perl -wMstrict -le "my $s = qq{a\nq}; print $s =~ m'^a$q' ? ' ' : 'NO ', 'match'; print $s =~ m'^a$q'm ? ' ' : 'NO ', 'match'; print $s =~ m'^a$\nq' ? ' ' : 'NO ', 'match'; print $s =~ m/^a$\nq/m ? ' ' : 'NO ', 'match'; print $s =~ m'^a$\nq'm ? ' ' : 'NO ', 'match'; " NO match NO match NO match NO match match

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2024-04-23 18:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found