Recently, we have changed from MySQL to PostgreSQL. Most of the queries have been translated except for the ones with the Mysql ‘REGEXP’ keyword:
MySQL (prepared statement):
SELECT * FROM table WHERE ? REGEXP identifier;
We have a ‘table’, and one of the columns is called ‘identifier’. This ‘identifier’ column contains the actual regular expression pattern.
So, instead of ‘hard-coding’ the regexp pattern in the query, it looks-up the identifier column for the pattern.
In Postgresql, we need to use the ‘~’ keyword instead of the ‘REGEXP’ one (which is MySQL only), but with Postgresql I cannot seem to extract the pattern from a column.
I’ve tried following queries without success:
SELECT * FROM table WHERE 'test' ~ "identifier";
ERROR: invalid regular expression: parentheses () not balanced
SELECT * FROM table WHERE "identifier" ~ 'test';
-> no results
for testing purposes I created a number of records where the “identifier” column contains ‘.*’ as value (regular expression for match everything), but still I do not get the appropriate result.
Help is very much welcome, thank you!
Maybe the problem is in one of the regexp strings? (As the error string says)
I’ve tested
And it works for me. Here is my SQL Fiddle. http://sqlfiddle.com/#!12/f245c/3
The problem can be in different regexp interpretation. Details about postgresql regexp here http://www.postgresql.org/docs/current/static/functions-matching.html
UPD Also read http://www.postgresql.org/docs/current/interactive/sql-syntax-lexical.html#SQL-SYNTAX-STRINGS for detail about postgresql strings. You need this to write regexp correctly.