How do I escape backslash and single quote or double quote in Python?

How do I escape a backslash and a single quote or double quote in Python?

For example:

Long string = '''some 'long' string \' and \" some 'escaped' strings'''
value_to_change = re.compile(A EXPRESION TO REPRESENT \' and \")
modified = re.sub(value_to_change, 'thevalue', Long_string)

## Desired Output
modified = '''some 'long' string thevalue and thevalue some 'escaped' strings'''

Solution 1:

How you did it

If your "long string" is read from a file (as you mention in a comment) then your question is misleading. Since you obviously don't fully understand how escaping works, the question as you wrote it down probably is different from the question you really have.

If these are the contents of your file (51 bytes as shown + maybe one or two end-of-line characters):

some 'long' string \' and \" some 'escaped' strings

then this is what it will look like in python:

>>> s1 = open('data.txt', 'r').read().strip()
>>> s1
'some \'long\' string \\\' and \\" some \'escaped\' strings'
>>> print s1
some 'long' string \' and \" some 'escaped' strings

What you wrote in the question will produce:

>>> s2 = '''some 'long' string \' and \" some 'escaped' strings'''
>>> s2
'some \'long\' string \' and " some \'escaped\' strings'
>>> print s2
some 'long' string ' and " some 'escaped' strings
>>> len(s)
49

Do you see the difference?

There are no backslashes in s2 because they have special meaning when you use them to write down strings in Python. They have no special meaning when you read them from a file.

If you want to write down a string that afterwards has a backslash in it, you have to protect the backslash you enter. You have to keep Python from thinking it has special meaning. You do that by escaping it - with a backslash.

One way to do this is to use backslashes, but often the easier and less confusing way is to use raw strings:

>>> s3 = r'''some 'long' string \' and \" some 'escaped' strings'''
'some \'long\' string \\\' and \\" some \'escaped\' strings'
>>> print s3
some 'long' string \' and \" some 'escaped' strings
>>> s1 == s3
True

How you meant it

The above was only to show you that your question was confusing.

The actual answer is a bit harder - when you are working with regular expressions, the backslash takes on yet another layer of special meaning. If you want to safely get a backslash through string escaping and through regex escaping to the actual regex, you have to write down multiple backslashes accordingly.

Furthermore, rules for putting single quotes (') in single-quoted raw strings (r'') are a bit tricky as well, so I will use a raw string with triple single-quotes (r'''''').

>>> print re.sub(r'''\\['"]''', 'thevalue', s1)
some 'long' string thevalue and thevalue some 'escaped' strings

The two backslashes stay two backslashes throughout string escaping and then become only one backslash without special meaning through regex escaping. In total, the regex says: "match one backslash followed by either a single-quote or a double-quote."

How it should be done

Now for the pièce de résistance: The previous is really a good demonstration of what jwz meant1. If you forget about regex (and know about raw strings), the solution becomes much more obvious:

>>> print s1.replace(r'\"', 'thevalue').replace(r"\'", 'thevalue')
some 'long' string thevalue and thevalue some 'escaped' strings

1 Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.

Solution 2:

The problem is that in your string \' and \" get converted to ' and ", so on your example as-is, you won't be able to match only \' without matching the single quotes around long.

But my understanding is that this data comes from a file so assuming you have your_file.txt containing

some 'long' string \' and \" some 'escaped' strings

you can replace \' and \" with following code:

import re

from_file = open("your_file.txt", "r").read()

print(re.sub("\\\\(\"|')", "thevalue", from_file))

Note the four slashes. Since this is a string \ gets converted to \ (as this is an escaped character). Then in the regular expression, the remaining \ gets again converted to \, as this is also regular experssion escaped character. Result will match a single slash and one of the " and ' quotes.

Solution 3:

is this what you want?

import re
Long_string = "some long string \' and \" some escaped strings"
value_to_change = re.compile( "'|\"" )
modified = re.sub(value_to_change , 'thevalue' , Long_string )
print modified 

Solution 4:

Keep in mind, all these strings are exactly the same:

Long_string = '''some long string \' and \" some escaped strings'''
Long_string = '''some long string ' and " some escaped strings'''
Long_string = """some long string ' and " some escaped strings"""
Long_string = 'some long string \' and \" some escaped strings'
Long_string = "some long string \' and \" some escaped strings"
Long_string = 'some long string \' and " some escaped strings'
Long_string = "some long string ' and \" some escaped strings"

There is no backslash character in any of them. So the regex you're looking for doesn't need to match a backslash and a quote, just a quote:

modified = re.sub("['\"]", 'thevalue', Long_string)

BTW: You also don't have to compile the regex before you use it, re.sub will accept a string regex as well as a compiled one.