2023-05-03 11:06:20 +02:00
|
|
|
== Why is this an issue?
|
|
|
|
|
2021-07-27 14:10:16 +02:00
|
|
|
include::../description.adoc[]
|
2021-04-28 18:08:03 +02:00
|
|
|
|
2023-05-03 11:06:20 +02:00
|
|
|
=== Noncompliant code example
|
2021-04-28 16:49:39 +02:00
|
|
|
|
2022-02-04 17:28:24 +01:00
|
|
|
[source,java]
|
2021-04-28 16:49:39 +02:00
|
|
|
----
|
|
|
|
String date = "01/02";
|
|
|
|
|
|
|
|
Pattern datePattern = Pattern.compile("(?<month>[0-9]{2})/(?<year>[0-9]{2})");
|
|
|
|
Matcher dateMatcher = datePattern.matcher(date);
|
|
|
|
|
|
|
|
if (dateMatcher.matches()) {
|
|
|
|
checkValidity(dateMatcher.group(1), dateMatcher.group(2)); // Noncompliant - numbers instead of names of groups are used
|
|
|
|
checkValidity(dateMatcher.group("day")); // Noncompliant - there is no group called "day"
|
|
|
|
}
|
|
|
|
|
|
|
|
// ...
|
|
|
|
|
|
|
|
String score = "14:1";
|
|
|
|
|
|
|
|
Pattern scorePattern = Pattern.compile("(?<player1>[0-9]+):(?<player2>[0-9]+)"); // Noncompliant - named groups are never used
|
|
|
|
Matcher scoreMatcher = scorePattern.matcher(score);
|
|
|
|
|
|
|
|
if (scoreMatcher.matches()) {
|
|
|
|
checkScore(score);
|
|
|
|
}
|
|
|
|
----
|
|
|
|
|
2021-04-28 18:08:03 +02:00
|
|
|
|
2023-05-03 11:06:20 +02:00
|
|
|
=== Compliant solution
|
2021-04-28 16:49:39 +02:00
|
|
|
|
2022-02-04 17:28:24 +01:00
|
|
|
[source,java]
|
2021-04-28 16:49:39 +02:00
|
|
|
----
|
|
|
|
String date = "01/02";
|
|
|
|
|
|
|
|
Pattern datePattern = Pattern.compile("(?<month>[0-9]{2})/(?<year>[0-9]{2})");
|
|
|
|
Matcher dateMatcher = datePattern.matcher(date);
|
|
|
|
|
|
|
|
if (dateMatcher.matches()) {
|
|
|
|
checkValidity(dateMatcher.group("month"), dateMatcher.group("year"));
|
|
|
|
}
|
|
|
|
|
|
|
|
// ...
|
|
|
|
|
|
|
|
String score = "14:1";
|
|
|
|
|
|
|
|
Pattern scorePattern = Pattern.compile("(?<player1>[0-9]+):(?<player2>[0-9]+)");
|
|
|
|
Matcher scoreMatcher = scorePattern.matcher(score);
|
|
|
|
|
|
|
|
if (scoreMatcher.matches()) {
|
|
|
|
checkScore(scoreMatcher.group("player1"));
|
|
|
|
checkScore(scoreMatcher.group("player2"));
|
|
|
|
}
|
|
|
|
----
|
|
|
|
Or, using dedicated variables instead of group names:
|
|
|
|
|
2022-02-04 17:28:24 +01:00
|
|
|
[source,java]
|
2021-04-28 16:49:39 +02:00
|
|
|
----
|
|
|
|
String score = "14:1";
|
|
|
|
|
|
|
|
String player = "([0-9]+)";
|
|
|
|
String gameScore = player + ":" + player;
|
|
|
|
|
|
|
|
Pattern scorePattern = Pattern.compile(gameScore);
|
|
|
|
Matcher scoreMatcher = scorePattern.matcher(score);
|
|
|
|
|
|
|
|
if (scoreMatcher.matches()) {
|
|
|
|
checkScore(score);
|
|
|
|
}
|
|
|
|
----
|
2021-04-28 18:08:03 +02:00
|
|
|
|
2021-09-20 15:38:42 +02:00
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
|
|
|
|
'''
|
|
|
|
== Implementation Specification
|
|
|
|
(visible only on this page)
|
|
|
|
|
2023-05-25 14:18:12 +02:00
|
|
|
=== Message
|
|
|
|
|
|
|
|
* not used: Use the named groups of this regex or remove the names
|
|
|
|
* numbers are used: Directly use 'name' instead of its number
|
|
|
|
|
|
|
|
|
|
|
|
=== Highlighting
|
|
|
|
|
|
|
|
* not used: the regex
|
|
|
|
* numbers are used:
|
|
|
|
** primary: the number
|
|
|
|
** secondary: the corresponding group in the regex
|
2021-09-20 15:38:42 +02:00
|
|
|
|
|
|
|
|
|
|
|
endif::env-github,rspecator-view[]
|