0

This is my python code line which is giving me invalid escape sequence '/' lint issue.

pattern = 'gs:\/\/([a-z0-9-]+)\/(.+)$'  # for regex matching

It is giving me out that error for all the backslash I used here . any idea how to resolve this ?

quicksilver
  • 289
  • 5
  • 11
  • 3
    You don't need to escape `/` in Python regular expressions. – Barmar May 10 '22 at 20:14
  • You should be using a raw string for a regular expression, so the backslashes will be passed through transparently to the regexp engine. – Barmar May 10 '22 at 20:15
  • @Barmar so how would the above pattern will look for regex matching? – quicksilver May 10 '22 at 20:16
  • 1
    @quicksilver, what do you mean by that question? `pattern = 'gs://([a-z0-9-]+)/(.+)$'` is enough on its own; it doesn't need anything else to "look for regex matching". The answer advising use of a raw string is good for the future so you can use backslashes in later versions of your regex, but your current regex doesn't strictly need it; all you need to do is take out the unnecessary backslashes. – Charles Duffy May 10 '22 at 20:26

1 Answers1

4

There's two issues here:

  1. Since this is not a raw string, the backslashes are string escapes, not regexp escapes. Since \/ is not a valid string escape sequence, you get that warning. Use a raw string so that the backslashes will be ignored by the string parser and passed to the regexp engine. See What exactly is a "raw string regex" and how can you use it?
  2. In some languages / is part of the regular expression syntax (it's the delimiter around the regexp), so they need to be escaped. But Python doesn't use / this way, so there's no need to escape them in the first place.

Use this:

pattern = r'gs://([a-z0-9-]+)/(.+)$'  # for regex matching
Barmar
  • 741,623
  • 53
  • 500
  • 612