Search code examples
pythonsonarqubesonarqube-scanpylint

Sonar Issue: Remove this "\", add another "\" to escape it, or make this a raw string


There is a rule in SonaeQube/SonarLint/SonarSource for backslashes:-

"\" should only be used as an escape character outside of raw strings

[https://rules.sonarsource.com/python/RSPEC-1717][1]

So now i am using regular expression like this :-

re= '\{(\d+)[,\-](\d+)\}': # Numbered pattern

SonarQube is giving issue is like : Remove this "\", add another "\" to escape it, or make this a raw string.

I cannot avoid the use of backslashes here, Please Suggest me how to solve this.


Solution

  • Just do as the suggestion says.

    Option 1: escape the escape characters with an additional \

    re = '\\{(\d+)[,\\-](\\d+)\\}'
    

    Option 2: make it a raw string

    re = r'\{(\d+)[,\-](\d+)\}'
    

    In this case, option 2 requires less changes (only the r prefix) and is easier to read.