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 '.'.

Why is this an issue?

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.

Exceptions

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.

Code examples

Noncompliant code example

String filename = "archive.tar.gz";
String[] parts = filename.split("."); // Noncompliant: matches every character instead of a literal '.'

Compliant solution

String filename = "archive.tar.gz";
String[] parts = filename.split("\\.");

Noncompliant code example

String filename = "report.2026.pdf";
String sanitized = filename.replaceAll(".", "_"); // Noncompliant: replaces every character
String first = filename.replaceFirst(".", "_"); // Noncompliant

Compliant solution

String filename = "report.2026.pdf";
String sanitized = filename.replace(".", "_");
String first = filename.replaceFirst("\\.", "_");

Noncompliant code example

Pattern dots = Pattern.compile("."); // Noncompliant: compiles a match-any-character pattern
boolean dotted = Pattern.matches(".", "a"); // Noncompliant
boolean oneChar = "a".matches("."); // Noncompliant

Compliant solution

Pattern dots = Pattern.compile("\\.");
boolean dotted = Pattern.matches("\\.", "a");
boolean oneChar = "a".matches("\\.");

Noncompliant code example

Pattern anyChar = Pattern.compile("."); // Noncompliant

Compliant solution

Pattern anyChar = Pattern.compile("."); // NOSONAR: "." intentionally matches any character

Resources

Documentation

Related rules