By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,270 Members | 1,703 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,270 IT Pros & Developers. It's quick & easy.

re.sub replacement text \-escapes woe

P: n/a
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
Jul 18 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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
Jul 18 '05 #2

P: n/a
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
Jul 18 '05 #3

P: n/a
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

Jul 18 '05 #4

P: n/a
Peter Otten <__*******@web.de> 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
Jul 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.