rspec/rules/S5857/python/rule.adoc
Nils Werner 6bed56fba9
Modify rule S5857: Add Python as covered language (#557)
* Modify rule S5857: Add Python as covered language

* Remove unsupported possessive quantifier from rule description and compliant solutions
2021-11-08 14:22:56 +00:00

31 lines
1.1 KiB
Plaintext

Using reluctant quantifiers (also known as lazy or non-greedy quantifiers) in patterns can often lead to needless backtracking, making the regex needlessly inefficient and potentially vulnerable to https://www.regular-expressions.info/catastrophic.html[catastrophic backtracking]. Particularly when using ``++.*?++`` or ``++.+?++`` to match anything up to some terminating character, it is usually a better idea to instead use a greedily or quantified negated character class containing the terminating character. For example ``++<.+?>++`` should be replaced with ``<[^>]*>`` or ``<[^>]+>``.
== Noncompliant Code Example
----
r'<.+?>'
r'".*?"'
----
== Compliant Solution
----
r'<[^>]+>'
r'"[^"]*"'
----
== Exceptions
This rule only applies in cases where the reluctant quantifier can easily be replaced with a negated character class. That means the repetition has to be terminated by a single character or character class. Patterns such as the following, where the alternatives without reluctant quantifiers are more complicated, are therefore not subject to this rule:
----
/<!--.*?-->/
-/\*.*?\*/-
----