3

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.

1
  • Why did the last version not work? Commented Sep 27, 2010 at 13:24

3 Answers 3

7

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.

Sign up to request clarification or add additional context in comments.

1 Comment

>>> 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\=\;\-
3

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

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

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

1 Comment

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”?
2

Try adding another backslash:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.