在正则表达式中转义方括号之前转义反斜线的问题

Issue with escaped antislash before an escaped square bracket in regex

当我在转义方括号之前转义反斜杠时,我遇到了正则表达式匹配问题

import re

def test_regex(test_number, regex_to_test, string_to_test):
    if re.match(regex_to_test, string_to_test) is None:
        print("Test #{0} FAILED ! Returned value was None".format(test_number))
    else:
        print("Test #{0} OK".format(test_number))

#              REGEX APPLIED   STRING        REAL STRING
test_regex(0, "\\{\\}",     "\{\}")     # \{\}
test_regex(1, "\\{\\\\}", "\{\\}")    # \{\} or :\{\} ???
test_regex(2, "\\{\\\\}", "\{\\}")   # \{\}
# Now we replace in test '{' and '}' respectively by '[' and ']'
test_regex(3, "\\[\\]",     "\[\]")     # \[\]
test_regex(4, "\\[\\\\]", "\[\\]")    # \[\] or :\[\] ???
test_regex(5, "\\[\\\\]", "\[\\]")   # \[\]
# Extra incomprehensible
test_regex(6, "\\{\\}",     "\{\}")       # {}
test_regex(7, "\\{\\}",   "\{\}")       # {}
# Now we replace in test '{' and '}' respectively by '[' and ']'
test_regex(8, "\\[\\]",     "\[\]")       # []
test_regex(9, "\\[\\]",   "\[\]")       # []

输出:

Test #0 OK
Test #1 OK
Test #2 OK
Test #3 FAILED ! Returned value was None
Test #4 FAILED ! Returned value was None
Test #5 FAILED ! Returned value was None
Test #6 OK
Test #7 OK
Test #8 FAILED ! Returned value was None
Test #9 FAILED ! Returned value was None

为什么从“{”和“}”切换到“[”和“]”会改变一切?

如何编辑我的正则表达式以使测试 #3 和 #4 像“{”和“}”一样工作?

在 python 中,在一个简单的字符串中,双反斜杠被解释为单反斜杠。

你应该尝试在参数中使用字符串前的"r"字母,它使python解释器,将其存储为原始字符串,例如:

test_regex(3, r"\\[\\]",     "\[\]")

输出:

Test #3 OK