Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > re.sub replacement text \-escapes woe

Reply
Thread Tools

re.sub replacement text \-escapes woe

 
 
Alexander Schmolck
Guest
Posts: n/a
 
      02-13-2004
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
 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      02-14-2004
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
 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      02-14-2004
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
 
Reply With Quote
 
David M. Wilson
Guest
Posts: n/a
 
      02-14-2004
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

 
Reply With Quote
 
Alexander Schmolck
Guest
Posts: n/a
 
      02-14-2004
Peter Otten <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ASPNET permissions woe - even when writing to a local drive that is mapped jimmyfishbean@yahoo.co.uk ASP .Net 3 09-12-2005 04:35 PM
Recursive nested elements woe GR33DY XML 3 06-25-2004 09:55 PM
XSL Recursive nested elements woe GR33DY XML 0 06-24-2004 09:34 AM
static routing woe's - please help ongicek Cisco 1 12-31-2003 04:21 PM
Attachment Woe slumpy Computer Support 3 07-09-2003 12:59 AM



Advertisments