2

I was trying to match a backslash using regex and thought that this could be done by using two double backslashes, using the former to escape the latter. However, when I run the code


path_str = r"\Animal_1-"
Match_backslash = re.search("[\\]", path_str)
print(Match_backslash)

I get the error message:

error: unterminated character set at position 0

But, when I use triple backslash:


path_str = r"\Animal_1-"
Match_backslash = re.search("[\\\]", path_str)
print(Match_backslash)

it for some reason works, can anyone explain why triple backslash is needed and why double backslash isn´t sufficient?

Leo
  • 38
  • 4
  • Does this answer your question? [Confused about backslashes in regular expressions](https://stackoverflow.com/questions/33582162/confused-about-backslashes-in-regular-expressions) – luxcem Dec 08 '22 at 15:47
  • 1
    The python string handling uses it as an escape and then also does regex. You could prefix `r` on the string in python to make it "raw" to remove one of these, e.g., `r"[\\]"`). – Dan Dec 08 '22 at 15:48
  • 2
    The string literal `"[\\]"` produces the string `[\]` due to Python's escaping rules, then the regex engine's escaping rules cause the remaining backslash to apply to the closing bracket. `r"[\\]"` or `"[\\\\]"` would be the proper ways to write the string literal so that the regex engine sees a double backslash, in other words something that it would treat as an actual character rather than the start of an escape sequence. Three backslashes happens to work as well, because the third one does not start any escape sequence that Python recognizes. – jasonharper Dec 08 '22 at 15:56

0 Answers0