Login or Sign up Help | Site Map
Connecting Tech Pros Worldwide

Replace special character

Question posted by: dillipkumar (Newbie) on May 9th, 2008 11:30 AM
Can somebody help me,

Take any string from command line prompt & replace what ever special character will get in that string that should be replaced with \ character.

Ex:
Enter any string: xyz/plks/abc.txt/$

Out put should be like this:
xyz\/plks\/abc\.txt\/\$

Thanks
Dillip
Would you like to answer this question?
Sign up for a free account, or Login (if you're already a member).
cnsabar's Avatar
cnsabar
Newbie
29 Posts
May 9th, 2008
11:47 AM
#2

Re: Replace special character
Hi.,

Try this.,
Code: ( text )
  1. $InputString=~s#(\W)#\\$1#gi;


Quote:
Originally Posted by dillipkumar
Can somebody help me,

Take any string from command line prompt & replace what ever special character will get in that string that should be replaced with \ character.

Ex:
Enter any string: xyz/plks/abc.txt/$

Out put should be like this:
xyz\/plks\/abc\.txt\/\$

Thanks
Dillip

Reply
KevinADC's Avatar
KevinADC
Expert
2,751 Posts
May 9th, 2008
07:05 PM
#3

Re: Replace special character
or try the quotemeta() function:

Code: ( text )
  1. $foo = 'xyz/.?.!@#$%^&*()_+/abc';
  2. $foo = quotemeta($foo);
  3. print $foo;

Reply
dillipkumar's Avatar
dillipkumar
Newbie
17 Posts
May 15th, 2008
04:53 AM
#4

Re: Replace special character
Quote:
Originally Posted by cnsabar
Hi.,

Try this.,
Code: ( text )
  1. $InputString=~s#(\W)#\\$1#gi;



Thanks~
Now it is working fine.

Reply
Reply
Not the answer you were looking for? Post your question . . .
170,099 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Top Perl Forum Contributors