Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

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

by Allasso (Monk)
on May 12, 2019 at 15:43 UTC ( #1233653=note: print w/replies, xml ) Need Help??


in reply to Re: 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?

This reduces the degrees of freedom of the ^ $ . operators
Maybe that hints at the following inconsistency I see with the "the end of the line (or before newline at the end)" rule?

Example

print('String: a\nb\nc\n' . "\n"); $text_1 = "a\nb\nc\n"; $text_1 =~ s@(\n)$@@s; print("----------\n>" . $1 . "<\n"); print("----------\n>" . $text_1 . "<\n");
Gives:
String: a\nb\nc\n ---------- > < ---------- >a b c<
In this case, the $ behaved like \z. Or another way to say it, in this case explicit \n matches where dot with s mod doesn't.

Replies are listed 'Best First'.
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
    ... 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://1233653]
help
Chatterbox?
and the web crawler heard nothing...

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



    Results (111 votes). Check out past polls.

    Notices?