All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sonar.l10n.py.rules.python.S6002.html Maven / Gradle / Ivy

There is a newer version: 4.23.0.17664
Show newest version

This rule raises an issue when a regex lookahead contradicts the rest of the regex.

Why is this an issue?

Lookahead assertions are a regex feature that makes it possible to look ahead in the input without consuming it. It is often used at the end of regular expressions to make sure that substrings only match when they are followed by a specific pattern.

For example, the following pattern will match an "a" only if it is directly followed by a "b". This does not consume the "b" in the process:

r"a(?=b)"

However, lookaheads can also be used in the middle (or at the beginning) of a regex. In that case there is the possibility that what comes after the lookahead contradicts the pattern inside the lookahead. Since the lookahead does not consume input, this makes the lookahead impossible to match and is a sign that there’s a mistake in the regular expression that should be fixed.

Code examples

Noncompliant code example

r"(?=a)b" # Noncompliant, the same character can't be equal to 'a' and 'b' at the same time

Compliant solution

r"(?<=a)b"
r"a(?=b)"




© 2015 - 2024 Weber Informatics LLC | Privacy Policy