Python:使用正则表达式捕获双反斜杠 C 字符

Python: Capturing double backslash C char with regex

我正在尝试通过 re 模块使用 Python 正则表达式捕获 '\'。我尝试使用:

back = re.compile(r"'\'")
print back.findall(line)

其中 line 是:char = '\';

但这并没有捕捉到任何东西。

我也试过:

 back = re.compile("'\\'")
 print back.findall(line)

无济于事。我的正则表达式有什么问题?

您需要转义反斜杠:

back = re.compile(r"'\\'")

代码:

>>> back = re.compile(r"'\\'")
>>> line = r"char = '\';"
>>> print back.findall(line)
["'\\'"]