Missing terminating ] for character class

A [ opened a character class, and no ] closed it before the pattern ended.

preg_match('/[a-z/', $value);
// preg_match(): Compilation failed: missing terminating ] for character class at offset 4

The slash after z closes the pattern, so the engine receives [a-z. It opens a class at the [ and reaches the end without a ]. The call returns false, and the offset is the end of the pattern. preg_replace() and the other functions print the same message under their own name.

Why PHP prints it

PCRE2 ends a class at the first ] that is neither escaped nor in first position. Three shapes leave the class open.

A ] in first position.

preg_match('/[]/', $value);
// preg_match(): Compilation failed: missing terminating ] for character class at offset 2

[] is not an empty class. PCRE2 takes the ] for a character of the class, then waits for its end. /[^]/ prints the same message.

The closing ] escaped.

preg_match('/[a-z\]/', $value);
// preg_match(): Compilation failed: missing terminating ] for character class at offset 6

The backslash makes the ] a character of the class.

A literal [ not escaped.

preg_match('/See [note/', $value);
// preg_match(): Compilation failed: missing terminating ] for character class at offset 9

The bracket belongs to the text to match. For PCRE2 it opens a class that nothing closes.

How to fix it

Close the class. When the bracket is text, escape it instead.

// The class closed
preg_match('/[a-z]/', 'abc');
// matches "a"

// A literal ] in the class, then the class closed
preg_match('/[a-z\]]/', ']');
// matches "]"

// A literal [: no class opens
preg_match('/See \[note/', 'See [note');
// matches "See [note"

When [] meant any character, write /./, which stops at a newline, or /[\s\S]/, which does not.

Where it hides

  • An empty variable in a generated pattern. '/[' . $chars . ']+' builds /[]+/ when $chars is empty, and PHP prints the message at offset 3.
  • A class closed after its group. In /([a-z)/ the ] and the ) are swapped. A ) inside a class is a character, so the class never closes. /([a-z])/ compiles.
  • A POSIX class with a bracket lost. /[[:alpha:]/ prints the message at offset 10: the class opened by the first [ needs two ].
  • Another language’s habit. JavaScript accepts [] and [^] as classes. PHP refuses both with this message.

What PHPRegex reports

  • regex.charclass.unclosed for /[a-z/: Unclosed character class "]" at end of input. The library gives the same offset as PHP, and adds the hint: Close the class with "]", or write "\[" to match a literal "[".

Open /[a-z/ in the playground: the Diagnostics tab shows the code, its message, and what PHP says.

Edit on GitHub Feedback