LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-27-2010, 09:53 PM   #1
android2009
LQ Newbie
 
Registered: Aug 2010
Posts: 16

Rep: Reputation: 0
[Perl] How can I escape all backslashes in a string without specify each one ?


Hello there,

I want to read a input from user and output something like 'input\b\c\d', which has to escape all backslashes if using double-quote. For instance, the following code would work.

Just curious if any other way I could do it without specify all backslashes? Since that takes much efforts when the sequence is long.

Code:
#!/usr/bin/perl

$letterA='A';

print "$letterA\\b\\c\\d';
Any idea is well appreciated,
Thanks!
 
Old 09-27-2010, 10:04 PM   #2
kurumi
Member
 
Registered: Apr 2010
Posts: 228

Rep: Reputation: 53
what is it you are trying to do to user input?
I would just break the input up and rejoin back using "\". you can also do a regex replacement to replace single characters
with "\"$1.

In Ruby
Code:
$ ruby -e 's="abc"; print "\\"+s.split(//).join("\\")  '
\a\b\c
You can do similarly with Perl.
 
Old 09-28-2010, 06:49 AM   #3
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by android2009 View Post
Hello there,

I want to read a input from user and output something like 'input\b\c\d', which has to escape all backslashes if using double-quote. For instance, the following code would work.

Just curious if any other way I could do it without specify all backslashes? Since that takes much efforts when the sequence is long.

Code:
#!/usr/bin/perl

$letterA='A';

print "$letterA\\b\\c\\d';
Any idea is well appreciated,
Thanks!
How about

Code:
$user_input =~ s/\\/\\\\/g;
?
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
escape string in bash script so it can be used in command line BuckRogers01 Linux - Software 15 08-12-2010 09:38 AM
awk escape character for colon in string quadmore Programming 2 02-27-2007 04:56 PM
shell script question: automatic \ of escape characters in string lagu2653 Programming 2 03-08-2006 07:58 AM
Handle escape characters in a string Helene Programming 7 05-01-2004 11:43 PM
Using escape string error in MySQL select command, any one can help me? myunicom Linux - General 2 02-16-2004 09:20 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:53 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration