1

I'm trying to create a regex to find (and then eventually replace) parts of strings in a PG DB. I'm using PSQL 9.0.4

I've tested my regex outside of PG and it works perfectly. However, it isn't playing well with PG. If anyone can help me understand what I'm doing wrong it would me much appreciated.

Regex:

{php}.*\n.*\n.*'mister_xx']\)\);.*\n} \n{\/php}

Postgres Query:

SELECT COUNT(*) FROM (SELECT * FROM "Table" WHERE "Column" ~ '{php}.*\n.*\n.*'mister_xx']\)\);.*\n} \n{\/php}') as x;

Postgres Response:

WARNING:  nonstandard use of escape in a string literal
LINE 1: ...M (SELECT * FROM "Table" WHERE "Column" ~ '{php}.*\n...
                                                             ^
HINT:  Use the escape string syntax for escapes, e.g., E'\r\n'.
ERROR:  syntax error at or near "mister_xx"
LINE 1: ..."Table" WHERE "Column" ~ '{php}.*\n.*\n.*'mister_x...
6
  • You need to escape single quotes around mister_xx in your query. Commented May 21, 2013 at 5:46
  • I've tried that, sadly it returns the same error. Commented May 21, 2013 at 6:00
  • I also tried escaping the { and } characters Commented May 21, 2013 at 6:06
  • 1
    Try to start from something simple. Like WHERE "Column" ~ '.*''mister_xx''.*'. It will find you any text, that has 'mister_xx' in it. Then just buid up your regexp from that point. Commented May 21, 2013 at 6:07
  • 1
    Your current regexp has too many errors to fix them at once. Commented May 21, 2013 at 6:08

2 Answers 2

1

In SQL, quotes are delimited as two quotes, for example:

'Child''s play'

Applying this to your regex makes it work:

SELECT COUNT(*)
FROM "Table"
WHERE "Column" ~ '{php}.*\n.*\n.*''mister_xx'']\)\);.*\n} \n{\/php}' as x;

Note also how the redundant subquery .

Sign up to request clarification or add additional context in comments.

Comments

1

You need to double escape the backslashes and add an E before the statement: SELECT * FROM "Table" WHERE "Column" ~ E'{php}\n.\n.*''mister_xx'']\)\);.*\n} \n{\/php}'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.