Skip to content
Advertisement

Python lint issue : invalid escape sequence ‘/’

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 ?

Advertisement

Answer

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
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement