Philipp Kühn
Philipp Kühn

Reputation: 1649

How to escape parentheses in a REGEXP clause

In my database I have some keys like this:

custom_field_languages(0)language
custom_field_languages(1)language
custom_field_languages(2)language
...

And I need all of them. But this doesn't work:

REGEXP '^custom_field_languages([0-9])language'

This also doesn't work.

REGEXP '^custom_field_languages\([0-9]\)language'

This works but isn't 100% accurate.

REGEXP '^custom_field_languages.[0-9].language'

What is the correct way to escape normal parentheses?

Upvotes: 3

Views: 2399

Answers (1)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726987

You can escape parentheses with square brackets, like this:

REGEXP '^custom_field_languages[(][0-9][)]language'

This is especially useful when you need to embed your query string into a language that provides its own interpretation for backslashes inside string literals.

Demo.

Upvotes: 6

Related Questions