You want a capture group around this so you can backreference to it for the closing one. So we match only pairs. `` re.match(r'("?)foobar\1', '"foobar"')``