0

Using the re library my problem is in this line

x = re.match("([A-Z]:|home∼|\.\.?)\\", txt)

Im trying to get a match with a disk "E:", "home∼", or a parent folder ".." followed by a backslash

The error goes as following:

Exception has occurred: error bad escape (end of pattern) at position 20

Am i doing something wrong? I think i´m doing something really dumb and don't realise it

Dorknob78
  • 43
  • 4
  • You probably meant to use a raw string `r"([A-Z]:|home∼|\.\.?)\\"` - note the `r` at the start. Otherwise the actual string value has only a single backslash at the end. – alani Sep 12 '20 at 22:12
  • Backslash is both a string escape and an RE escape. After the string parser converts double escape to single escape, there's nothing for the escape to escape when the RE parser processes it. – Barmar Sep 12 '20 at 22:13
  • ```\\``` in a string literal gives you a ```\``` in your string. A ```\``` on its own in a regular expression is a bad escape because the regex engine doesn't know what you're trying to escape with it. – khelwood Sep 12 '20 at 22:13

0 Answers0