== Why is this an issue?

When a back reference in a regex refers to a capturing group that hasn't been defined yet (or at all), it can never be matched. Named back references throw a ``++PatternSyntaxException++`` in that case; numeric back references fail silently when they can't match, simply making the match fail.


When the group is defined before the back reference but on a different control path (like in ``++(.)|\1++`` for example), this also leads to a situation where the back reference can never match.


=== Noncompliant code example

[source,java]
----
Pattern.compile("\\1(.)"); // Noncompliant, group 1 is defined after the back reference
Pattern.compile("(.)\\2"); // Noncompliant, group 2 isn't defined at all
Pattern.compile("(.)|\\1"); // Noncompliant, group 1 and the back reference are in different branches
Pattern.compile("(?<x>.)|\\k<x>"); // Noncompliant, group x and the back reference are in different branches
----


=== Compliant solution

[source,java]
----
Pattern.compile("(.)\\1");
Pattern.compile("(?<x>.)\\k<x>");
----


ifdef::env-github,rspecator-view[]

'''
== Implementation Specification
(visible only on this page)

include::../message.adoc[]

include::../highlighting.adoc[]

'''
== Comments And Links
(visible only on this page)

include::../comments-and-links.adoc[]

endif::env-github,rspecator-view[]
