473,386 Members | 1,733 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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
Jul 18 '05 #1
4 1270
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Lennin | last post by:
Hello! I am looking for a SQL server Full-Text replacement. Does anybody know an alternative product that does full-text on db columns? This is my situation: I have large databases 40gb...
3
by: J Trost | last post by:
I was wondering if anyone knows if it is possible to do basic string replacement using XSLT even though the strings being replaced may contain "<" and ">". Here is my problem: I need to be able...
53
by: Kerberos | last post by:
I followed Dan Cederholm's image replacement tutorial, to replace a header tag by a logo. The h1 is clickable if no CSS is applied but it I replace it by the logo, the area isn't clickable anymore...
3
by: Vibha Tripathi | last post by:
Hi Folks, I put a Regular Expression question on this list a couple days ago. I would like to rephrase my question as below: In the Python re.sub(regex, replacement, subject)...
1
by: fleemo17 | last post by:
For increased accessibility, I've replaced "display:none" with the Off-Left method of hiding my CSS drop-down menus because the Jaws screen reader doesn't see any of the menus hidden with...
2
by: brian | last post by:
Hi, before coming to .NET, I utilized regular expressions mostly in JScript / JavaScript and also in my favorite text editor: TextPad (www.textpad.com) I don't know about JScript/JavaScript, but...
20
by: Paul D. Boyle | last post by:
Hi all, There was a recent thread in this group which talked about the shortcomings of fgets(). I decided to try my hand at writing a replacement for fgets() using fgetc() and realloc() to read...
16
by: Alan Silver | last post by:
Hello, At one time, image replacement was recommended as being a good way to have well structured markup, whilst giving the user something pleasant visually. However, I have seen a few articles...
0
by: rmeshksar | last post by:
Hi, I would like to do text replacement using RegEx and use the following statement: Regex.Replace(input, pattern, replacement, RegexOptions.IgnoreCase) It works fine in all cases except in the...
5
by: padmaneha | last post by:
How to omit replacement in somepart of text using regular expression? For ex: If i want to replace mumbai with mumbai/aaa.com but if i have an anchor tag <a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.