Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re^3: How to match last character of string, even if it happens to be a newline?

by AnomalousMonk (Chancellor)
on May 12, 2019 at 16:56 UTC ( #1233657=note: print w/replies, xml ) Need Help??


in reply to Re^2: How to match last character of string, even if it happens to be a newline?
in thread How to match last character of string, even if it happens to be a newline?

... in this case explicit \n matches where dot with s mod doesn't.

That's because  \n$ requires a match with newline, but  .$ allows the leftmost position of a match with dot (with /s) to be before the newline. Dot will match newline in the presence of  $ if it is the only match available:

c:\@Work\Perl\monks>perl -wMstrict -le "use Data::Dump qw(pp); ;; for my $s (qq{yz}, qq{yz\n}, qq{\n}) { $s =~ m{ (.) $ }xms; printf qq{in %s matched %s \n}, pp($s), pp($1); } " in "yz" matched "z" in "yz\n" matched "z" in "\n" matched "\n"
(The  /m modifier makes no difference in these example strings.)

The thing to remember about regular expressions is that there are a lot of things to remember about regular expressions. If you have a chance to reduce the amount of stuff to remember, even if only by a little, take it. That's why I advise (per TheDamian's regex PBPs) using  \A \z \Z for all your start- and end-of-string anchoring needs, and using  ^ $ only for embedded newline matching.

... inconsistency ...

For me, it's not so much inconsistency as mind-boggling complexity. And again, I come back to the point that if you can reduce the complexity of what you're dealing with even a little, you're ahead of the game.


Give a man a fish:  <%-{-{-{-<

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (9)
As of 2019-06-26 16:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Is there a future for codeless software?



    Results (110 votes). Check out past polls.

    Notices?