Escaping escape characters in variables (in regexs)

J

johnnykimble

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
 
D

David Squire

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?

perldoc perlfunc

- search for quotemeta.

See also \Q and \E in perldoc perlre.


DS
 
B

Ben Morrow

Nope, that's not a problem. The delimiters only delimit: once Perl knows
where the ends of the regex are, they are no longer special.
^
This match ends here..........|, probably not what you wanted. Is this
your real code?

You would be much better off with an XML module like XML::Simple and
MIME::Base64, I suspect.

'Doesn't work' is about the least helpful thing you could say. What
*does* it do, and how is this different from what you were expecting?
Anyone know what I can do to get this to work

perldoc perlfunc

- search for quotemeta.

....except none of the characters in Base64 are meta.

Ben
 
T

Tad McClellan

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


Please don't post code with syntax errors.

(Unless your question is about the syntax errors.)

$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?


You do not not need to match before substituting.

$text = s#<SomeNode>(.+?)</SomeNode>#<SomeNode>hello</SomeNode>#;
 
T

Tad McClellan

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.


That can't be it, because slash is never "meta" in data.

It is only meta in code.

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>/;
^^^ ^^^
^^^ ^^^

Is this your real code?

If so, then why aren't you asking about the error messages that
it produces?

Have you seen the Posting Guidelines that are posted here frequently?

$base64stuff = $1;

Then I try to perform a substitution with:

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

But this doesn't work.


Of course not, your program must compile before you can execute it,
and it must execute before you can see if it "works".

Please post a short and complete program *that we can run* that
illustrates your problem.

Anyone know what I can do to get this to work
properly?


It already works properly for me.

Here is a short and complete program *that you can run* that
illustrates your lack of problem:


--------------------
#!/usr/bin/perl
use warnings;
use strict;

my $text = '<SomeNode>skdsSda3321/2=///==asda==////adasd/213/'
. 'dw/ASDASd/ad</SomeNode>';

if ( $text =~ m#<SomeNode>(.+?)</SomeNode># ) {
my $base64stuff = $1;
$text =~ s/$base64stuff/hello/;
}

print "$text\n";
 
T

Tad McClellan

David Squire said:
(e-mail address removed) wrote:


Note that there are not any regex metacharacters in that data.

perldoc perlfunc

- search for quotemeta.

See also \Q and \E in perldoc perlre.


Knowing about quotemeta is certainly a good thing, but it has
nothing to do with the OP's problem because slashes are not
meta in regular expressions, and so need no escaping.
 
J

John Kimble

Thanks, applying quotemeta to the first part of the substitution
operator worked great.
 
B

Ben Morrow

Quoth Ben Morrow said:
...except none of the characters in Base64 are meta.

Sorry, of course '+' is meta and in Base64... what I meant was, 'None of
the characters in the above piece of Base64 are meta, so presuming this
is the OP's real code that's not the problem'.

Ben
 
D

David Squire

Ben said:
Sorry, of course '+' is meta and in Base64... what I meant was, 'None of
the characters in the above piece of Base64 are meta, so presuming this
is the OP's real code that's not the problem'.

.... and indeed Base64 is not uniquely defined - the last few characters
beyond 0-9, A-Z, a-z differ in various conventions - though now the
version in the MIME RFC seems to be the de facto standard.


DS
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top