moderated >> backslash escapes

by Ykstort » Fri, 21 Oct 2005 20:43:16 GMT

Is there a way I can use regex to convert backslashes \\ to backslash
escapes \ that is quicker than:
s/\\n/\n/g;
s/\\r/\r/g;
....etc....

s/\\/\/g;
and
s/\\(.)/\$1/g;
obviously don't work.

moderated >> backslash escapes

by James D. White » Wed, 26 Oct 2005 05:59:12 GMT


Try eval('"' . ... . '"')

The following program:

$a = 'a\\\\nb\\\\nc\\\\07d\\\\n';
printf "%d='%s'\n", length($a), $a;

$b = eval ( '"' . $a . '"' );
printf "%d='%s'\n", length($b), $b;

$c = eval ( '"' . $b . '"' );
printf "%d='%s'\n", length($c), $c;

produces the output:

17='a\\nb\\nc\\07d\\n'
13='a\nb\nc\07d\n'
8='a
b
cd #<-- this is really "c", bell, "d"
'




James D. White ( XXXX@XXXXX.COM )
Director of Bioinformatics
Department of Chemistry and Biochemistry/ACGT
University of Oklahoma
101 David L. Boren Blvd., SRTC 2100
Norman, OK 73019
Phone: (405) 325-4912, FAX: (405) 325-7762

moderated >> backslash escapes

by Mark Jason Dominus » Wed, 26 Oct 2005 11:03:01 GMT

"James D. White":

That fails in many, many cases, and is really risky unless you're
absolutely certain what the input will look like. For example, it
fails when the argument is '"' or "\".system('rm -rf /').\"";

The best method I know is essentially what the original poster
suggested:

%escape = ('n' => "\n",
't' => "\t",
'r' => "\r",
'\\' => "\\",
'\"' => "\"",
# ...
);

sub unescape {
my $s = shift;
$s =~ s/\\(.)/$escape{$1}/g;
$s;
}

You still have to do something extra if you want to handle \x0d and
\007 also.

moderated >> backslash escapes

by Ykstort » Wed, 26 Oct 2005 11:14:52 GMT

Thanks.
That seems to work and noone can muck around with system("evil")
because of the quotes.
The only security problem is that people can find out the values of any
variables, if they know the names.

moderated >> backslash escapes

by Ykstort » Wed, 26 Oct 2005 11:30:57 GMT

>That fails in many, many cases, and is really risky unless you're
I hadn't thought of that...

moderated >> backslash escapes

by Jgen Exner » Wed, 26 Oct 2005 11:34:34 GMT


I have no idea what a backslash escape is but do you just want to substitute
a double backslash with a single backslash?
That's pretty trivial:

$_="backslash>>\\\\<<";
print "double $_ \n";
s/\\\\/\\/;
print "single $_ \n";

jue

moderated >> backslash escapes

by wisefamily » Thu, 27 Oct 2005 03:23:32 GMT


The following code will work:

s/\\[^deghijkmopqsvwyzABCDFGHIJKMNOPQRSTUVWXYZ]/eval qq(return
"\\$1")/eg;

The eval function is the key. You take the character after the
backslash, put a backslash before it, and evaluate it. I can't think
of any other way, because the characters \n, \r, etc., are just
characters; perl doesn't know that they are written like "\n" or "\r",
etc. One problem is that certain characters like \d, \g, or \h are not
characters. You will get a warning message if you try to use them.
Invalid escapes are:

\d \g \h \i \j \k \m \o \p \q \s \v \w \y \z \A \B \C \D \F \G \H \I \J
\K \M \O \P \Q \R \S \T \U \V \W \X \Y \Z

Also, if you escape e or N like "\e" or "\N", you get a warning
message:
"Use of uninitialized value in substitution iterator at Escape.pl line
46, <STDIN> line 1."

I excluded them in the regular expression (I also excluded \e and \N):

s/\\[^deghijkmopqsvwyzABCDFGHIJKMNOPQRSTUVWXYZ]/eval qq(return
"\\$1")/eg;

One thing to watch out for is the \b (backspace) character. It deletes
the previous character. This statement:

print "Here is a backspace\b character.\n"

would print this:

Here is a backspac character.

which may or may not be a problem. If it is a problem, exclude it in
the regular expression:

s/\\[^bdeghijkmopqsvwyzABCDFGHIJKMNOPQRSTUVWXYZ]/eval qq(return
"\\$1")/eg;

Hope this helps,
David

moderated >> backslash escapes

by Ykstort » Thu, 27 Oct 2005 06:40:37 GMT

No,
Backslash escapes are for special characters like \t, \n, \r, \x[2 hex
digits], \x{[several hex digits]}, \0[octal char (two digits?)], \l,
\L, \u, \U, \Q, \E, \a, \e and maybe a few others
have a look at perlre manpage for more info

moderated >> backslash escapes

by John W. Krahn » Fri, 28 Oct 2005 06:11:40 GMT


Have you actually tried it to confirm that it works?


John
--
use Perl;
program
fulfillment

moderated >> backslash escapes

by wisefamily » Fri, 28 Oct 2005 07:01:11 GMT


I tested s/\\(.)/eval qq(return "\\$1")/eg before, and it worked.
However, I tested s/\\[^deghijkmopqsvwyzABCDFGHIJKMNOPQRSTUVWXYZ]/eval
qq(return "\\$1")/eg because you mentioned it, and I realize that it
should be:
s/\\([^cdeghijkmopqsvwyzABCDFGHIJKMNOPQRSTUVWXYZ])/eval qq(return
"\\$1")/eg
or
s/\\([^bcdeghijkmopqsvwyzABCDFGHIJKMNOPQRSTUVWXYZ])/eval qq(return
"\\$1")/eg
if you don't want the \b character.

David

moderated >> backslash escapes

by Ykstort » Fri, 28 Oct 2005 22:23:38 GMT

perl -e "$_ = 'foo\\nbar\\n'; s/\\(.)/eval qq(return "\\$1")/eg; print"
gives
fooSCALAR(0x225164)barSCALAR(0x225ed8)
?

moderated >> backslash escapes

by Ykstort » Sat, 29 Oct 2005 05:35:58 GMT

nevermind
It's because I put it in single quotes.

Similar Threads

1. Interpolation of backslash-escapes

I am looking for an way to interpolate backslash-sequences
within a string with the usual perl semantics, e.g.
$s='1\t\2\t3\na\ b c' should become:
'1<tab>2<tab>3
a b c'

Things I tried were for example
 $s= eval('' . "$val"); # (hoping to trigger the normal interpolation) or
$s=~ s/\\(.)/"\\$1"/eg;
but somehow i couldn't get it right ...

Can anybody think of an elegant solution?

Regards,
                  Peter Daum

2. Escaping backslashes in 'HERE documents'? - Perl

3. Escaping escape characters in variables (in regexs)

Hi all,

I've got to replace some characters in Base64 encoded data in a text
file but I'm having problems with the s/// operator mainly, I presume,
because of the presence of the '/' character in the input data.

For example, say I have a file containing the following:

<SomeNode>skdsSda3321/2=///==asda==////adasd/213/dw/ASDASd/ad</SomeNode>

I can pull out the Base64 here with:

$text = m/<SomeNode>(.+?)</SomeNode>/;
$base64stuff = $1;

Then I try to perform a substitution with:

$text =~ s/$base64stuff/hello/;

But this doesn't work. Anyone know what I can do to get this to work
properly?

Thanks,
JK

4. hyperlink excel 2007 backslash Internet Explorer 7

5. Perl quoting convention that avoids excessive backslashes

Perl lets you quote strings like this:

$foo = qq%This string has both "quotes" and 'apostrophes'%;

to avoid excessive backslashing.

Does ruby have anything similar?

-- 
We're just a Bunch Of Regular Guys, a collective group that's trying
to understand and assimilate technology. We feel that resistance to
new ideas and technology is unwise and ultimately futile.

6. problem using backslash on brackets in regular expressions - Perl

7. String is Missing Backslash

Hello

I am working on a perl script and inside the script I am building a 
string. However the backslash does not appear.

Here is the code:

$DirCommand = "dir".$CurrentDrive.":\ /S"; where $DirCommand = "c"

I am expecting the results to be dir c:\ /S but my results are dir c: /S

This affects the execution of the command.

What did I miss putting in?

Thanks,
Andrew

8. backslash on windows - Perl