Bad Regular Expression Syntax

ID

java.bad_regex_syntax

Severity

high

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Reliability

Language

Java

Tags

reliability

Description

Reports calls to Pattern.compile(), String.matches(), String.split(), String.replaceAll(), or String.replaceFirst() whose first argument is a string literal containing an invalid regular expression. An invalid regex always throws PatternSyntaxException at runtime.

Rationale

A syntactically invalid regex will cause every invocation to fail with a PatternSyntaxException. This is a guaranteed runtime error that should be caught at development time.

// Bad -- unclosed character class
Pattern.compile("[unclosed");

Remediation

Fix the regular expression syntax.

Pattern.compile("[a-z]+");