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

Matching character

by rag-sree (Initiate)
on Jul 21, 2013 at 14:35 UTC ( #1045511=perlquestion: print w/ replies, xml ) Need Help??
rag-sree has asked for the wisdom of the Perl Monks concerning the following question:

I,m very much new to perl. How can I match special character \ at the end of a string?

Comment on Matching character
Re: Matching character
by vinoth.ree (Parson) on Jul 21, 2013 at 14:47 UTC
    $str = "testing\"; $str =~ /\\$/;

    All is well

      This is easy to do when typing code directly into a node, you would catch this quickly but for the new monks:

      $str = "testing\";

      The backslash in this case will escape the double quote (") and with warnings turned on you get this:

      Can't find string terminator '"' anywhere before EOF at C:\b\perlmonks\regex\1045512.pl line 4.

      Without warnings you get "Bareword found where operator expected at ..."

      use strict; use warnings; my $str = "testing\\"; print "matched!" if $str =~ /\\$/;

        Lol Lotus1

        You are right. I did not test. Thanks


        All is well
Re: Matching character
by mtmcc (Hermit) on Jul 21, 2013 at 15:43 UTC
    It might be useful for you to read through perlre.
Re: Matching character
by choroba (Abbot) on Jul 21, 2013 at 21:19 UTC
    You can use substr:
    $string = 'abcd\\'; print "Matched\n" if '\\' eq substr $string, -1;
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2014-07-13 07:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (248 votes), past polls