Message 337274 - Python tracker (original) (raw)

"\-" is equivalent to the raw string r"-" (that it, it has one backslash, followed by a hyphen). \X (where X is any ASCII non-letter, non-digit) matches the character itself (the escape does nothing except ensure the punctuation doesn't have any special regex meaning). So your pattern is equivalent to "-". Since re.match has an implicit anchor at the beginning of the string (making it roughly like "^-"), the string "-" doesn't match.

Use raw strings consistently for your regular expressions to reduce the number of rounds of deescaping. re.match(r"\-", "\-") works as you expected.