Reprocess Escape Sequences

Discussion in 'Ruby' started by Nathaniel Madura, Apr 22, 2010.

  1. I am getting a string that has escape sequences in it, and I would like
    to print it to the screen after reprocessing the escape sequences. Maybe
    I have missed something obvious but I haven't seen anything in the docs
    that suggest how to do this. To replicate the behaviour I create a
    string in irb like so:

    $ irb
    >> str = 'some \n\r string \t with \r escape \n sequences'

    => "some \\n\\r string \\t with \\r escape \\n sequences"
    >> eval "puts str"

    some \n\r string \t with \r escape \n sequences
    => nil
    >> puts "%s" % str

    some \n\r string \t with \r escape \n sequences
    => nil

    I recognize that I can simply do a gsub:

    >> puts str.gsub(/\\n/, "\n")

    some
    \r string \t with \r escape
    sequences
    => nil

    but that would mean I would have to pattern match all sequences, and it
    seems like there should be a better way

    Any suggestions?
    --
    Posted via http://www.ruby-forum.com/.
    Nathaniel Madura, Apr 22, 2010
    #1
    1. Advertising

  2. 2010/4/22 Nathaniel Madura <>:
    > I am getting a string that has escape sequences in it, and I would like
    > to print it to the screen after reprocessing the escape sequences. Maybe
    > I have missed something obvious but I haven't seen anything in the docs
    > that suggest how to do this. To replicate the behaviour I create a
    > string in irb like so:
    >
    > $ irb
    >>> str =3D 'some \n\r string \t with \r escape \n sequences'

    > =3D> "some \\n\\r string \\t with \\r escape \\n sequences"
    >>> eval "puts str"

    > some \n\r string \t with \r escape \n sequences
    > =3D> nil


    If you use eval you rather need to do something like this

    irb(main):004:0> str =3D 'some \n\r string \t with \r escape \n sequences'
    =3D> "some \\n\\r string \\t with \\r escape \\n sequences"
    irb(main):005:0> puts eval('"'+str+'"')
    some
    escape with
    sequences
    =3D> nil
    irb(main):006:0>


    >>> puts "%s" % str

    > some \n\r string \t with \r escape \n sequences
    > =3D> nil
    >
    > I recognize that I can simply do a gsub:
    >
    >>> puts str.gsub(/\\n/, "\n")

    > some
    > \r string \t with \r escape
    > =A0sequences
    > =3D> nil
    >
    > but that would mean I would have to pattern match all sequences, and it
    > seems like there should be a better way


    Actually doing a replacement would probably be my preferred way
    because of the security implications of eval.

    You can do it in one go

    irb(main):008:0> PAT =3D {'n'=3D>"\n",'t'=3D>"\t",'r'=3D>"\r"}
    =3D> {"n"=3D>"\n", "t"=3D>"\t", "r"=3D>"\r"}
    irb(main):009:0> str.gsub(/\\(.)/){|m| PAT[$1]}
    =3D> "some \n\r string \t with \r escape \n sequences"

    Kind regards

    robert

    --=20
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
    Robert Klemme, Apr 23, 2010
    #2
    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. Olaf Petzold

    vhdl textio and escape sequences

    Olaf Petzold, Nov 26, 2005, in forum: VHDL
    Replies:
    1
    Views:
    3,388
    Mike Treseler
    Nov 28, 2005
  2. Harald Kirsch
    Replies:
    0
    Views:
    320
    Harald Kirsch
    Nov 19, 2004
  3. Kit

    Escape sequences and printing

    Kit, Sep 16, 2003, in forum: C Programming
    Replies:
    5
    Views:
    486
    Matt Gregory
    Sep 17, 2003
  4. slomo
    Replies:
    5
    Views:
    1,503
    Duncan Booth
    Dec 2, 2007
  5. Paul Thompson

    Reprocess page after document.write

    Paul Thompson, Jan 31, 2005, in forum: Javascript
    Replies:
    3
    Views:
    133
    Robert
    Feb 1, 2005
Loading...

Share This Page