Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Appending backslash to a string

by garu (Scribe)
on Aug 10, 2007 at 23:36 UTC ( [id://631901]=perlquestion: print w/replies, xml ) Need Help??

garu has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

I'd like to append a single character to a string only if it's not there already. Now, if the char is 'c' (for example) I can do something like:

$char = 'c'; $str = 'bla'; $str =~ s/$char?$/$char/; print "$str\n"
and it works like a charm, printing "blac" whether $str is "bla" or "blac" already.

However, if I want to append a backslash instead of a simple character:

$char = '\\'; $str = 'bla'; $str =~ s/$char?$/$char/; print "$str\n"
it does not work as I'd expect. I even tried '\\\\' but this way I get two backslashes (*as I'd expect*)

What am I doing wrong? I just ran out of ideas... Any help would be greatly appreciated.

Thanks!

Replies are listed 'Best First'.
Re: Appending backslash to a string
by eric256 (Parson) on Aug 10, 2007 at 23:41 UTC

    If you want to include a variable inside a regex, but don't want the data in the variable treated as part of the regex then you wrap it in \Q \E (which i beleve stand for quote and end, its at least how i rememebr)

    #!/usr/bin/perl use strict; use warnings; my $char = '\\'; my $str = 'bla'; $str =~ s/\Q$char\E?$/$char/; print "$str\n";

    ___________
    Eric Hodges
      Damn, I knew that! And to think I used it a couple pages up the original code... guess I was tired.

      Anyway, thanks for the quick response! :-)

      I have searched high and low for a solution to a similar problem I am grappling with. I am writing a complex Perl script, part of which attempts to escape all url forward slashes by preceding them with backslashes via regex. I am able to replace forward slashes with backslashes, using variations of the following:

      $test =~ tr [\\] [\/];
      But I want my output to look like the following:
      http:\/\/www.myurl.com\/me.html

      Any advice would be extremely helpful. Thanks!

        Please note that you're replying to a 13-year old thread, you'll probably get better exposure by posting a new question in SoPW, following the advice in How do I post a question effectively? Anyway, tr/// only replaces single characters, you may want s///g instead (or in this case, s{}{}g to avoid Leaning Toothpick Syndrome). However, in my experience, an escape usually doesn't come alone (do you need to escape backslashes too?), so it would be much better if you told us why you need to escape slashes, perhaps there's a much better method than regexes that we would miss otherwise.

        Here is a simple SSCCE showing how to achieve what you have asked for.

        use strict; use warnings; use Test::More tests => 1; my $have = 'http://www.myurl.com/me.html'; my $want = 'http:\/\/www.myurl.com\/me.html'; $have =~ s#/#\\/#g; is $have, $want;

        Note however that I agree with haukex that under most circumstances the result which you say you want is maybe not so useful and that this does sound rather like an XY Problem.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2024-04-26 00:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found