26 lines
815 B
Plaintext
26 lines
815 B
Plaintext
== Why is this an issue?
|
|
|
|
The regex function `re.sub` can be used to perform a search and replace based on regular expression matches. The `repl` parameter can contain references to capturing groups used in the `pattern` parameter. This can be achieved with `\n` to reference the n'th group.
|
|
|
|
When referencing a nonexistent group an error will be thrown for Python < 3.5 or replaced by an empty string for Python >= 3.5.
|
|
|
|
=== Noncompliant code example
|
|
|
|
[source,python]
|
|
----
|
|
re.sub(r"(a)(b)(c)", r"\1, \9, \3", "abc") # Noncompliant - result is an re.error: invalid group reference
|
|
----
|
|
|
|
=== Compliant solution
|
|
|
|
[source,python]
|
|
----
|
|
re.sub(r"(a)(b)(c)", r"\1, \2, \3", "abc")
|
|
----
|
|
|
|
== Resources
|
|
|
|
* https://docs.python.org/3.10/library/re.html#re.sub[re.sub] - Python Documentation
|
|
|
|
include::../implementation.adoc[]
|