http://www.perlmonks.org?node_id=742259


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

Without any modifiers $ matches the end of the string. But for practical reasons it sort of ignores a newline there. The practical reason is that you often read in lines from files and want to match without first having to chomp the line. A convenience for very small scripts and one liners. Here is a relevant citation from the perlre man page:

By default, the "^" character is guaranteed to match only the beginning of the string, the "$" character only the end (or before the newline at the end), and Perl does certain optimizations with the assumption that the string contains only one line. Embedded newlines will not be matched by "^" or "$". You may, however, wish to treat a string as a multi-line buffer, such that the "^" will match after any newline within the string (except if the newline is the last character in the string), and "$" will match before any newline. At the cost of a little more overhead, you can do this by using the /m modifier on the pattern match operator. (Older programs did this by setting $*, but this practice has been removed in perl 5.9.)

  • Comment on Re: When exactly do Perl regex's require a full match on a string?