how to convert a string to an escaped string

G

Gregory Toomey

Gunnar said:
Glad you think so. Don't top post, btw.


Yes, there is such a function.

perldoc -f quotemeta

Why didn't you just look it up instead of posting here?

Thats a stupid statement.

This group reeks of arrogance. People ask legitmate questions and are
"baited" by the regulars.

gtoomey
 
L

Lowell Kirsh

I want to convert:
blah blah "foo" blah
to
blah\ blah\ \"foo\"\ blah

Is there an easy way to do it?

Lowell
 
L

Lowell Kirsh

Very funny... How about a perl function that can convert a string to
another string in which special chars are represented as escaped
versions of themselves...
 
G

Gunnar Hjalmarsson

Lowell said:
Very funny...

Glad you think so. Don't top post, btw.
How about a perl function that can convert a string to
another string in which special chars are represented as escaped
versions of themselves...

Yes, there is such a function.

perldoc -f quotemeta

Why didn't you just look it up instead of posting here?
 
G

Gunnar Hjalmarsson

Gregory said:
Thats a stupid statement.

This group reeks of arrogance. People ask legitmate questions and
are "baited" by the regulars.

First: My limited Perl knowledge doesn't qualify me to be called a
"regular".

Since OP wondered if there is a function in Perl that escapes special
characters, the natural first step should reasonably have been to look
in perldoc perlfunc. If he had done so, he would most likely have
found it.

Asking hundreds of people for help with looking up things in the
documentation is not a legitimate step, at least not when the answer
can be as easily found as in this case. Pointing out that is neither
stupid nor arrogant.
 
J

Jon Landenburer

Lowell Kirsh said:
I want to convert:
blah blah "foo" blah
to
blah\ blah\ \"foo\"\ blah

Is there an easy way to do it?

Lowell

#!/bin/perl
$A = 'dogs "bark" and cats "meow"';
print "$A\n";
$A=~s- -\\ -g;
print "$A\n";

give
!./a.pl
dogs "bark" and cats "meow"
dogs\ "bark"\ and\ cats\ "meow"
[Hit return to continue]

$A=~ s- -\\ -g

=~ says evaluate this expression
s says substitute
- next charater after s is a delimiter
the three delimiters define a before and after
the before is a " "
the after is a "\"
g says global. do it for all occurrentces of the before pattern
(instead of the first.
 
B

Bart Lateur

Lowell said:
I want to convert:
blah blah "foo" blah
to
blah\ blah\ \"foo\"\ blah

Is there an easy way to do it?

quotemeta()?

Otherwise, try

s/(?=[CHARLIST])/\\/g;

with CHARLIST replaced by a list of characters/ range of characters, for
example

s/(?=[\\\s\"])/\\/g;

to escape whitespace, quotes, and backslashes.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,439
Members
44,829
Latest member
PIXThurman

Latest Threads

Top