org.sonar.l10n.py.rules.python.S5994.html Maven / Gradle / Ivy
Why is this an issue?
Possessive quantifiers in Regex patterns like below improve performance by eliminating needless backtracking:
?+ , *+ , ++ , {n}+ , {n,}+ , {n,m}+
But because possessive quantifiers do not keep backtracking positions and never give back, the following sub-patterns should not match only similar
characters. Otherwise, possessive quantifiers consume all characters that could have matched the following sub-patterns and nothing remains for the
following sub-patterns.
Noncompliant code example
import re
pattern1 = re.compile(r"a++abc", re.DOTALL) # Noncompliant, the second 'a' never matches
pattern2 = re.compile(r"\d*+[02468]", re.DOTALL) # Noncompliant, the sub-pattern "[02468]" never matches
Compliant solution
import re
pattern1 = re.compile(r"aa++bc", re.DOTALL) # Compliant, for example it can match "aaaabc"
pattern2 = re.compile(r"\d*+(?<=[02468])", re.DOTALL) # Compliant, for example, it can match an even number like "1234"
© 2015 - 2024 Weber Informatics LLC | Privacy Policy