Escaping escape characters in variables (in regexs)

Discussion in 'Perl Misc' started by johnnykimble@gmail.com, Jun 27, 2006.

  1. Guest

    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
     
    , Jun 27, 2006
    #1
    1. Advertising

  2. David Squire Guest

    wrote:
    > 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
     
    David Squire, Jun 27, 2006
    #2
    1. Advertising

  3. Ben Morrow Guest

    Quoth David Squire <>:
    > wrote:
    > > 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.


    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.

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

    ^
    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.

    > > $base64stuff = $1;
    > >
    > > Then I try to perform a substitution with:
    > >
    > > $text =~ s/$base64stuff/hello/;
    > >
    > > But this doesn't work.


    '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
    > > properly?

    >
    > perldoc perlfunc
    >
    > - search for quotemeta.


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

    Ben

    --
    You poor take courage, you rich take care:
    The Earth was made a common treasury for everyone to share
    All things in common, all people one. []
    'We come in peace'---the order came to cut them down.
     
    Ben Morrow, Jun 27, 2006
    #3
  4. <> wrote:

    > $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>#;


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Jun 27, 2006
    #4
  5. <> wrote:

    > 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";
    --------------------


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Jun 27, 2006
    #5
  6. David Squire <> wrote:
    > wrote:



    >> For example, say I have a file containing the following:
    >>
    >> <SomeNode>skdsSda3321/2=///==asda==////adasd/213/dw/ASDASd/ad</SomeNode>



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


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


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



    > 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.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Jun 27, 2006
    #6
  7. John Kimble Guest

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

    David Squire wrote:
    > wrote:
    > > 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
     
    John Kimble, Jun 27, 2006
    #7
  8. Ben Morrow Guest

    Quoth Ben Morrow <>:
    >
    > Quoth David Squire <>:
    > > wrote:
    > > > 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:

    <snip>
    > >
    > > perldoc perlfunc
    > >
    > > - search for quotemeta.

    >
    > ...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

    --
    Every twenty-four hours about 34k children die from the effects of poverty.
    Meanwhile, the latest estimate is that 2800 people died on 9/11, so it's like
    that image, that ghastly, grey-billowing, double-barrelled fall, repeated
    twelve times every day. Full of children. [Iain Banks]
     
    Ben Morrow, Jun 27, 2006
    #8
  9. David Squire Guest

    Ben Morrow wrote:
    > Quoth Ben Morrow <>:
    >> Quoth David Squire <>:
    >>> wrote:
    >>>> 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:

    > <snip>
    >>> perldoc perlfunc
    >>>
    >>> - search for quotemeta.

    >> ...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'.


    .... 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
     
    David Squire, Jun 27, 2006
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. kramer31

    java compiler errors on regexs

    kramer31, Jul 8, 2008, in forum: Java
    Replies:
    4
    Views:
    321
    kramer31
    Jul 11, 2008
  2. Chris Seberino
    Replies:
    1
    Views:
    526
    Hao Lian
    Sep 12, 2009
  3. Chris Seberino
    Replies:
    4
    Views:
    516
    Robert Kern
    Sep 13, 2009
  4. Jon Maz

    Escaping escape characters in JScript

    Jon Maz, Sep 14, 2004, in forum: ASP General
    Replies:
    14
    Views:
    248
    Lasse Reichstein Nielsen
    Sep 17, 2004
  5. Replies:
    5
    Views:
    111
Loading...

Share This Page