Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

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

by jwkrahn (Monsignor)
on May 12, 2019 at 18:48 UTC ( #1233663=note: print w/replies, xml ) Need Help??


in reply to How to match last character of string, even if it happens to be a newline?

You don't really need a regular expression just to get the last character in a string:

$ perl -e'use Data::Dumper; $Data::Dumper::Useqq = 1; my $text_1 = "a\ +nb\nc\n"; print Dumper $text_1, "Last character: " . substr $text_1, +-1' $VAR1 = "a\nb\nc\n"; $VAR2 = "Last character: \n";
  • Comment on Re: How to match last character of string, even if it happens to be a newline?
  • Download Code

Replies are listed 'Best First'.
Re^2: How to match last character of string, even if it happens to be a newline?
by LanX (Archbishop) on May 12, 2019 at 19:03 UTC
    On a side note :

    chop does the same job but is destructive and requires an lvalue.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

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 20:58 UTC
    indeed!

Log In?
Username:
Password:

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

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



    Results (81 votes). Check out past polls.

    Notices?
    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!