35 lines
953 B
Plaintext
35 lines
953 B
Plaintext
== Why is this an issue?
|
|
|
|
include::../description.adoc[]
|
|
|
|
=== Noncompliant code example
|
|
[source,python]
|
|
----
|
|
re.search(r"Jack|Peter|", "John") # Noncompliant - will match an empty string
|
|
re.search(r"Jack||Peter", "John") # Noncompliant - will match an empty string
|
|
----
|
|
=== Compliant solution
|
|
[source,python]
|
|
----
|
|
re.search(r"Jack|Peter", "John") # returns false
|
|
----
|
|
|
|
=== Exceptions
|
|
|
|
One could use an empty alternation to make a regular expression group optional. Rule will not report on such cases.
|
|
|
|
[source,python]
|
|
----
|
|
re.search(r"mandatory(-optional|)", "mandatory")
|
|
re.search(r"mandatory(-optional|)", "mandatory-optional")
|
|
----
|
|
|
|
However, if there is a quantifier after the group the issue will be reported as using both `|` and quantifier is redundant.
|
|
|
|
[source,python]
|
|
----
|
|
re.search(r"mandatory(-optional|)?", "mandatory-optional") # Noncompliant - using both `|` inside the group and `?` for the group.
|
|
----
|
|
|
|
include::../implementation.adoc[]
|