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

3
  • 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. Commented Sep 12, 2020 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. Commented Sep 12, 2020 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. Commented Sep 12, 2020 at 22:13

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.