re.sub replacement text \-escapes woe

Discussion in 'Python' started by Alexander Schmolck, Feb 13, 2004.

  1. Sorry if I missed something obvious, but how do you do this more
    intelligently?

    def escape(s):
    return re.sub(r'([${}\\])', r'\ \1', s).replace('\\ ', '\\')

    'as
    Alexander Schmolck, Feb 13, 2004
    #1
    1. Advertising

  2. Alexander Schmolck

    Peter Otten Guest

    Alexander Schmolck wrote:

    > Sorry if I missed something obvious, but how do you do this more
    > intelligently?
    >
    > def escape(s):
    > return re.sub(r'([${}\\])', r'\ \1', s).replace('\\ ', '\\')
    >
    > 'as


    Interesting. No direct attack, but another workaround:

    re.sub(r'([${}\\])', lambda m: '\\' + m.group(), s)

    Peter
    Peter Otten, Feb 14, 2004
    #2
    1. Advertising

  3. Alexander Schmolck

    Peter Otten Guest

    Peter Otten wrote:

    > Alexander Schmolck wrote:
    >
    >> Sorry if I missed something obvious, but how do you do this more
    >> intelligently?
    >>
    >> def escape(s):
    >> return re.sub(r'([${}\\])', r'\ \1', s).replace('\\ ', '\\')
    >>
    >> 'as

    >
    > Interesting. No direct attack, but another workaround:
    >
    > re.sub(r'([${}\\])', lambda m: '\\' + m.group(), s)


    re.sub(r'([${}\\])', r'\\\1', s)

    Either that or I'm slash-blind tonight...

    Peter
    Peter Otten, Feb 14, 2004
    #3
  4. Alexander Schmolck wrote:
    > Sorry if I missed something obvious, but how do you do this more
    > intelligently?
    >
    > def escape(s):
    > return re.sub(r'([${}\\])', r'\ \1', s).replace('\\ ', '\\')
    >


    re.escape? :)


    David
    David M. Wilson, Feb 14, 2004
    #4
  5. Peter Otten <> writes:

    > Peter Otten wrote:
    >
    > > Alexander Schmolck wrote:
    > >
    > >> Sorry if I missed something obvious, but how do you do this more
    > >> intelligently?
    > >>
    > >> def escape(s):
    > >> return re.sub(r'([${}\\])', r'\ \1', s).replace('\\ ', '\\')
    > >>
    > >> 'as

    > >
    > > Interesting. No direct attack, but another workaround:
    > >
    > > re.sub(r'([${}\\])', lambda m: '\\' + m.group(), s)

    >
    > re.sub(r'([${}\\])', r'\\\1', s)
    >
    > Either that or I'm slash-blind tonight...


    D'oh -- slash-blindness is what happened to me (presumably because I was
    unconsciously expecting a raw string when I tested the above in the
    interactive console before I posted and thus incorrectly dismissed the
    result).

    Thanks

    'as
    Alexander Schmolck, Feb 14, 2004
    #5
    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. Replies:
    3
    Views:
    515
    Paul Clement
    Sep 12, 2005
  2. GR33DY
    Replies:
    0
    Views:
    576
    GR33DY
    Jun 24, 2004
  3. GR33DY
    Replies:
    3
    Views:
    855
  4. Ben
    Replies:
    2
    Views:
    864
  5. Lawrence D'Oliveiro

    Death To Sub-Sub-Sub-Directories!

    Lawrence D'Oliveiro, May 5, 2011, in forum: Java
    Replies:
    92
    Views:
    1,974
    Lawrence D'Oliveiro
    May 20, 2011
Loading...

Share This Page