In Java regular expressions, . is a metacharacter that matches any single character, not a literal period.
Passing "." to regex APIs such as String.split, String.replaceAll, String.replaceFirst,
String.matches, Pattern.compile, or Pattern.matches therefore does not treat the argument as a dot separator.
Developers almost always intended a literal '.'.
String.split and Pattern.compile interpret their pattern argument as a regular expression. A pattern that is exactly
"." matches every character.
This is especially surprising with String.split: "archive.tar.gz".split(".") matches each character as a delimiter.
Trailing empty strings are discarded, so the result is an empty array rather than ["archive", "tar", "gz"].
The same pattern is just as wrong with replacement APIs. "report.2026.pdf".replaceAll(".", "x") replaces every character with
x, instead of replacing only the dots.
To match a literal period, escape the metacharacter ("\\."), quote it (Pattern.quote(".")), or use a non-regex API such
as String.replace.
This rule does not raise an issue when Pattern.compile is called with the Pattern.LITERAL flag, because that flag treats
the pattern as a literal string.
If matching any character is intentional, add an inline // NOSONAR: "." intentionally matches any character comment so the intent is
explicit.
String filename = "archive.tar.gz";
String[] parts = filename.split("."); // Noncompliant: matches every character instead of a literal '.'
String filename = "archive.tar.gz";
String[] parts = filename.split("\\.");
String filename = "report.2026.pdf";
String sanitized = filename.replaceAll(".", "_"); // Noncompliant: replaces every character
String first = filename.replaceFirst(".", "_"); // Noncompliant
String filename = "report.2026.pdf";
String sanitized = filename.replace(".", "_");
String first = filename.replaceFirst("\\.", "_");
Pattern dots = Pattern.compile("."); // Noncompliant: compiles a match-any-character pattern
boolean dotted = Pattern.matches(".", "a"); // Noncompliant
boolean oneChar = "a".matches("."); // Noncompliant
Pattern dots = Pattern.compile("\\.");
boolean dotted = Pattern.matches("\\.", "a");
boolean oneChar = "a".matches("\\.");
Pattern anyChar = Pattern.compile("."); // Noncompliant
Pattern anyChar = Pattern.compile("."); // NOSONAR: "." intentionally matches any character
String.split
methodPattern.quote
method