PostgreSQL 17.12Lab-verified · 2026-11-29

PostgreSQL 2201B: invalid regular expression

A regex special character (+, (, [) was used as plain text, so PostgreSQL could not compile the pattern.

PostgreSQL 17.12
2201B: invalid regular expression: quantifier operand invalid

Jump to a cause

  1. 01A + at the start of the pattern (phone numbers like +82)
  2. 02An unescaped ( when searching for text that contains parentheses
  3. 03A character class [ without the closing ]

Lab-verifiedReproduced and fixed on PostgreSQL 17.12 · 2026-11-29 · container image postgres:17

The ~ operator, regexp_replace and the other regex functions compile the pattern string first. If the pattern is broken, the query fails with 2201B before reading a single row.

The detail after the colon tells you what is wrong: quantifier operand invalid, parentheses () not balanced or brackets [] not balanced. These details stay in English even with Korean or Japanese lc_messages. Below are the three cases we reproduced on PostgreSQL 17, each with the fixed pattern actually run.

01A + at the start of the pattern (phone numbers like +82)

Searching for international numbers with phone ~ '^+82' fails with quantifier operand invalid. In a regex, + means "one or more of the previous item", and right after ^ there is nothing for it to repeat.

Escape it with a backslash: '^\+82'. With standard_conforming_strings on (the default), you write a single backslash inside the literal. The fixed query returned row 1 (+82-10-1234-5678). If you only need a prefix match, LIKE '+82%' returned the same row without any regex.

Setup
Setuprun as lab_a
CREATE TABLE customers (id int PRIMARY KEY, name text, phone text);
INSERT INTO customers VALUES (1, 'Kim (HQ)', '+82-10-1234-5678'), (2, 'Lee', '010-9876-5432');
OUTPUT
CREATE TABLE
INSERT 0 2
Reproducerun as lab_a
SELECT id, phone FROM customers WHERE phone ~ '^+82';
✕ ERROR
ERROR:  2201B: invalid regular expression: quantifier operand invalid
Checkrun as lab_a
SELECT id, phone FROM customers WHERE phone LIKE '+82%';
OUTPUT
 id |      phone
----+------------------
  1 | +82-10-1234-5678
(1 row)

Characters to escape when you mean them literally: . + * ? ( ) [ ] { } ^ $ | \

After the fixrun as lab_a
SELECT id, phone FROM customers WHERE phone ~ '^\+82';
✓ SUCCESS
 id |      phone
----+------------------
  1 | +82-10-1234-5678
(1 row)

02An unescaped ( when searching for text that contains parentheses

Looking for names that contain (HQ with name ~ '(HQ' fails with parentheses () not balanced, because ( opens a group that never closes.

Escape it as '\(HQ'; the query then found Kim (HQ). When you are searching for a fixed string, not a pattern, strpos(name, '(HQ') > 0 gave the same result and needs no escaping at all.

Setup
Setuprun as lab_a
CREATE TABLE customers (id int PRIMARY KEY, name text, phone text);
INSERT INTO customers VALUES (1, 'Kim (HQ)', '+82-10-1234-5678'), (2, 'Lee', '010-9876-5432');
OUTPUT
CREATE TABLE
INSERT 0 2
Reproducerun as lab_a
SELECT id, name FROM customers WHERE name ~ '(HQ';
✕ ERROR
ERROR:  2201B: invalid regular expression: parentheses () not balanced
Checkrun as lab_a
SELECT id, name FROM customers WHERE strpos(name, '(HQ') > 0;
OUTPUT
 id |   name
----+----------
  1 | Kim (HQ)
(1 row)

For user input, prefer strpos or LIKE over building a regex from the raw text.

After the fixrun as lab_a
SELECT id, name FROM customers WHERE name ~ '\(HQ';
✓ SUCCESS
 id |   name
----+----------
  1 | Kim (HQ)
(1 row)

03A character class [ without the closing ]

Stripping non-digits with regexp_replace(phone, '[^0-9', '', 'g') fails with brackets [] not balanced. The class [^0-9 is missing its ].

With the bracket closed, '[^0-9]', the same query returned 821012345678 and 01098765432.

Setup
Setuprun as lab_a
CREATE TABLE customers (id int PRIMARY KEY, name text, phone text);
INSERT INTO customers VALUES (1, 'Kim (HQ)', '+82-10-1234-5678'), (2, 'Lee', '010-9876-5432');
OUTPUT
CREATE TABLE
INSERT 0 2
Reproducerun as lab_a
SELECT id, regexp_replace(phone, '[^0-9', '', 'g') AS digits FROM customers;
✕ ERROR
ERROR:  2201B: invalid regular expression: brackets [] not balanced
Checkrun as lab_a
SELECT id, phone FROM customers ORDER BY id;
OUTPUT
 id |      phone
----+------------------
  1 | +82-10-1234-5678
  2 | 010-9876-5432
(2 rows)
After the fixrun as lab_a
SELECT id, regexp_replace(phone, '[^0-9]', '', 'g') AS digits FROM customers ORDER BY id;
✓ SUCCESS
 id |    digits
----+--------------
  1 | 821012345678
  2 | 01098765432
(2 rows)

FAQ

What does "quantifier operand invalid" mean in PostgreSQL?

A quantifier (+, *, ?, {n}) has nothing before it to repeat, for example ^+82. Escape it with a backslash (^\+82) if you mean the literal character.

How do I escape special characters in a PostgreSQL regex?

Put a backslash before them inside the pattern, like '\+' or '\('. With the default standard_conforming_strings = on, one backslash is enough in a normal string literal.

Can I avoid regex when I just search for a fixed string?

Yes. LIKE '+82%' and strpos(name, '(HQ') > 0 returned the same rows on this page without any escaping of + or (.

Is the 2201B detail message translated?

Only the first part. With Korean or Japanese lc_messages the prefix is translated, but the detail such as brackets [] not balanced stays in English.

How we verify

Each scenario starts from fresh database users. A script runs the setup, confirms the exact error code, applies the fix and runs the statement again. The output shown is what the database printed, saved by the script.

lab_a and lab_b are ordinary roles created for the test, each owning a schema of the same name; postgres is the superuser. PostgreSQL 17.12