tags:

views:

90

answers:

3

Hello, i have the following problem. I want to escape all special characters in a python string.

str='eFEx-x?k=;-'

re.sub("([^a-zA-Z0-9])",r'\\1', str)

'eFEx\\1x\\1k\\1\\1\\1'

str='eFEx-x?k=;-'

re.sub("([^a-zA-Z0-9])",r'\1', str)

'eFEx-x?k=;-'

re.sub("([^a-zA-Z0-9])",r'\\\1', str)

I can't seem to win here. '\1' indicates the special character and i want to add a '\' before this special character. but using \1 removes its special meaning and \\1 also does not help.

+7  A: 

Use r'\\\1'. That's a backslash (escaped, so denoted \\) followed by \1.

To verify that this works, try:

str = 'eFEx-x?k=;-'
print re.sub("([^a-zA-Z0-9])",r'\\\1', str)

This prints:

eFEx\-x\?k\=\;\-

which I think is what you want. Don't be confused when the interpreter outputs 'eFEx\\-x\\?k\\=\\;\\-'; the double backslashes are there because the interpreter quotes it output, unless you use print.

larsmans
yes, i was confused with the interpreter output!
Cheezo
>>> re.sub("([^a-zA-Z0-9])",r'\\\1', str)'eFEx\\-x\\?k\\=\\;\\-'>>> str='eFEx-x?k=;-'>>> print re.sub("([^a-zA-Z0-9])",r'\\\1', str)eFEx\-x\?k\=\;\-
Cheezo
+1  A: 

Try adding another backslash:

s = 'eFEx-x?k=;-'
print re.sub("([^a-zA-Z0-9])",r'\\\1', s)
Gintautas Miliauskas
+3  A: 

Why don't you use re.escape()?

str = 'eFEx-x?k=;-'
re.escape(str)

'eFEx\\-x\\?k\\=\\;\\-'
eumiro
This is certainly what you'd want if “special characters” means “characters that have special meaning to regex”. `str.encode('string-escape')` might be another possibility, if it's “characters that are special to Python string literals”. Who knows what's “special”?
bobince