re.sub replacement text \-escapes woe

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alexander Schmolck

    re.sub replacement text \-escapes woe

    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
  • Peter Otten

    #2
    Re: re.sub replacement text \-escapes woe

    Alexander Schmolck wrote:
    [color=blue]
    > 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[/color]

    Interesting. No direct attack, but another workaround:

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

    Peter

    Comment

    • Peter Otten

      #3
      Re: re.sub replacement text \-escapes woe

      Peter Otten wrote:
      [color=blue]
      > Alexander Schmolck wrote:
      >[color=green]
      >> 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[/color]
      >
      > Interesting. No direct attack, but another workaround:
      >
      > re.sub(r'([${}\\])', lambda m: '\\' + m.group(), s)[/color]

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

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

      Peter

      Comment

      • David M. Wilson

        #4
        Re: re.sub replacement text \-escapes woe

        Alexander Schmolck wrote:[color=blue]
        > 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('\\ ', '\\')
        >[/color]

        re.escape? :)


        David

        Comment

        • Alexander Schmolck

          #5
          Re: re.sub replacement text \-escapes woe

          Peter Otten <__peter__@web. de> writes:
          [color=blue]
          > Peter Otten wrote:
          >[color=green]
          > > Alexander Schmolck wrote:
          > >[color=darkred]
          > >> 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[/color]
          > >
          > > Interesting. No direct attack, but another workaround:
          > >
          > > re.sub(r'([${}\\])', lambda m: '\\' + m.group(), s)[/color]
          >
          > re.sub(r'([${}\\])', r'\\\1', s)
          >
          > Either that or I'm slash-blind tonight...[/color]

          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

          Comment

          Working...