Declarent has asked for the wisdom of the Perl Monks concerning the following question:

I'm recieving text input and I find that it has escape characters in it that I want to remove.

Under more and print, you obviously can't see these characters, but if you write out the contents of the variable to a file and vim it, you can see something like:
text^e^e^e^e
I'm using this to try and strip the characters:
$var=~s/someregex//g
Where someregex has been: \005,\cE,\ce,\x005,\x0x005
No luck. Anyone know what this mysterious escape character is and how I can get rid of it? Note that it only appears on the end of the string, and that the number of them is random.

Declarent

  • Comment on Regex question: How do I remove an escape code from text?

Replies are listed 'Best First'.
Re: Regex question: How do I remove an escape code from text?
by jsprat (Curate) on Mar 20, 2003 at 18:00 UTC
    You opened it in vim? Position the cursor under the '^e' and type "ga" (or ":as[cii]", if that's easier to remember). In the command line you will see the ascii value of the character.

      I opened it in vim, hit ga and guess what? It's a 0x00. Removed it via /\x00/ and now I'm 5x5.

      As usual, you Monks rock!

      D

Re: Regex question: How do I remove an escape code from text?
by dga (Hermit) on Mar 20, 2003 at 17:29 UTC

    I have both of the following work.

    tr/\005//d; tr/\x05//d;

    This will remove control-E from the variable ($_ in this case) but not the 2 character sequence '^' . 'e'

Re: Regex question: How do I remove an escape code from text?
by robartes (Priest) on Mar 20, 2003 at 17:15 UTC
    \x05 should work:
    $ echo 'text^E^E^E^E' |perl -ne 's/\x05//g;print;'|od -c 0000000 t e x t \n 0000005

    CU
    Robartes-

      Unfortunately this doesn't work. While I know that escape codes don't care about case, this shows up as ^e, rather than ^E and it resists all versions of 05 that I can throw at it. I'm thinking now that maybe it's something else and not that escape code. Is there a way to match it outside of hex? Also, it doesn't show up as regular text and does not match to \^e.

        You should run a sample of the input through od and see whats really there.

        I would recommend the -c and or -a switches to get 005 or enq respectively if its a control-e