Skip to main content
 首页 » 编程设计

python之Sonar Issue : Remove this "\", 添加另一个 "\"对其进行转义,或将其设为原始字符串

2024年11月24日24oomusou

SonaeQube/SonarLint/SonarSource 中有一个反斜杠规则:-

"\" should only be used as an escape character outside of raw strings 
 
[https://rules.sonarsource.com/python/RSPEC-1717][1] 

所以现在我正在使用这样的正则表达式:-

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

SonarQube 给出的问题是:删除这个“\”,添加另一个“\”来转义它,或者将其设为原始字符串。

我不能避免在这里使用反斜杠,请建议我如何解决这个问题。

请您参考如下方法:

按照建议去做。

选项 1:使用附加的 \

转义转义字符
re = '\\{(\d+)[,\\-](\\d+)\\}' 

选项 2:将其设为原始字符串

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

在这种情况下,选项 2 需要较少的更改(仅 r 前缀)并且更易于阅读。