0

I have written simple code in Python to match the string using pattern object:

import re

my_pattern = re.compile(r'a\\b')
my_string = 'a\\b'
if my_pattern.match(my_string):
    print("Pattern matches the string")

This code indeed print the specified text, but I do not know why. I passed to re.compile() a raw string with double backslash, while matched string contains only one so it shouldn't be matched. I thought that such pattern object would match the string only if it contains an and b separated by double backslash.

Timus
  • 10,974
  • 5
  • 14
  • 28
  • 1
    See: https://stackoverflow.com/questions/33582162/confused-about-backslashes-in-regular-expressions and https://stackoverflow.com/questions/4025482/cant-escape-the-backslash-in-a-regular-expression – slothrop May 06 '23 at 15:55

0 Answers0