如何使用单个反斜杠转义字符串的特殊字符

时间:2022-07-02 22:24:40

I'm trying to escape the characters -]\^$*. each with a single backslash \.

我试图逃避角色 - ] \ ^ $ *。每个都有一个反斜杠\。

For example the string: ^stack.*/overflo\w$arr=1 will become:

例如字符串:^ stack。* / overflo \ w $ arr = 1将变为:

\^stack\.\*/overflo\\w\$arr=1

What's the most efficient way to do that in Python?

在Python中最有效的方法是什么?

re.escape double escapes which isn't what I want:

re.escape double escapes这不是我想要的:

'\\^stack\\.\\*\\/overflow\\$arr\\=1'

I need this to escape for something else (nginx).

我需要这个以逃避其他东西(nginx)。

5 个解决方案

#1


57  

This is one way to do it (in Python 3.x):

这是一种方法(在Python 3.x中):

escaped = a_string.translate(str.maketrans({"-":  r"\-",
                                          "]":  r"\]",
                                          "\\": r"\\",
                                          "^":  r"\^",
                                          "$":  r"\$",
                                          "*":  r"\*",
                                          ".":  r"\."}))

For reference, for escaping strings to use in regex:

作为参考,用于转义在正则表达式中使用的字符串:

import re
escaped = re.escape(a_string)

#2


10  

Just assuming this is for a regular expression, use re.escape.

假设这是一个正则表达式,请使用re.escape。

#3


4  

Simply using re.sub might also work instead of str.maketrans. And this would also work in python 2.x

简单地使用re.sub也可以代替str.maketrans。这也适用于python 2.x.

>>> print(re.sub(r'(\-|\]|\^|\$|\*|\.|\\)',lambda m:{'-':'\-',']':'\]','\\':'\\\\','^':'\^','$':'\$','*':'\*','.':'\.'}[m.group()],"^stack.*/overflo\w$arr=1"))
\^stack\.\*/overflo\\w\$arr=1

#4


4  

re.escape doesn't double escape. It just looks like it does if you run in the repl. The second layer of escaping is caused by outputting to the screen.

re.escape不会双重逃脱。如果你在repl中运行它看起来就像它。第二层转义是由输出到屏幕引起的。

When using the repl, try using print to see what is really in the string.

使用repl时,尝试使用print来查看字符串中的内容。

$ python
>>> import re
>>> re.escape("\^stack\.\*/overflo\\w\$arr=1")
'\\\\\\^stack\\\\\\.\\\\\\*\\/overflo\\\\w\\\\\\$arr\\=1'
>>> print re.escape("\^stack\.\*/overflo\\w\$arr=1")
\\\^stack\\\.\\\*\/overflo\\w\\\$arr\=1
>>>

#5


0  

Utilize the output of built-in repr to deal with \r\n\t and process the output of re.escape is what you want:

利用内置repr的输出来处理\ r \ n \ t并处理re.escape的输出是你想要的:

re.escape(repr(a)[1:-1]).replace('\\\\', '\\')

#1


57  

This is one way to do it (in Python 3.x):

这是一种方法(在Python 3.x中):

escaped = a_string.translate(str.maketrans({"-":  r"\-",
                                          "]":  r"\]",
                                          "\\": r"\\",
                                          "^":  r"\^",
                                          "$":  r"\$",
                                          "*":  r"\*",
                                          ".":  r"\."}))

For reference, for escaping strings to use in regex:

作为参考,用于转义在正则表达式中使用的字符串:

import re
escaped = re.escape(a_string)

#2


10  

Just assuming this is for a regular expression, use re.escape.

假设这是一个正则表达式,请使用re.escape。

#3


4  

Simply using re.sub might also work instead of str.maketrans. And this would also work in python 2.x

简单地使用re.sub也可以代替str.maketrans。这也适用于python 2.x.

>>> print(re.sub(r'(\-|\]|\^|\$|\*|\.|\\)',lambda m:{'-':'\-',']':'\]','\\':'\\\\','^':'\^','$':'\$','*':'\*','.':'\.'}[m.group()],"^stack.*/overflo\w$arr=1"))
\^stack\.\*/overflo\\w\$arr=1

#4


4  

re.escape doesn't double escape. It just looks like it does if you run in the repl. The second layer of escaping is caused by outputting to the screen.

re.escape不会双重逃脱。如果你在repl中运行它看起来就像它。第二层转义是由输出到屏幕引起的。

When using the repl, try using print to see what is really in the string.

使用repl时,尝试使用print来查看字符串中的内容。

$ python
>>> import re
>>> re.escape("\^stack\.\*/overflo\\w\$arr=1")
'\\\\\\^stack\\\\\\.\\\\\\*\\/overflo\\\\w\\\\\\$arr\\=1'
>>> print re.escape("\^stack\.\*/overflo\\w\$arr=1")
\\\^stack\\\.\\\*\/overflo\\w\\\$arr\=1
>>>

#5


0  

Utilize the output of built-in repr to deal with \r\n\t and process the output of re.escape is what you want:

利用内置repr的输出来处理\ r \ n \ t并处理re.escape的输出是你想要的:

re.escape(repr(a)[1:-1]).replace('\\\\', '\\')