0

I want to match a list of special characters ('-&) for a postgresql query. Through an online regex editor I came to the pattern [\'\-\&\\]. But when I use that in my query I get an syntax error:

connection.execute("SELECT index, name FROM test21 WHERE test21.name  ~ '[\'''\-\&\\]'")

results ins

ProgrammingError: (ProgrammingError) syntax error at or near "\"
LINE 1: test21 WHERE test21.name  ~ '['''\-\&\]'
                                         ^

I found other answers, like https://stackoverflow.com/a/25925429/380038, but these match all special characters.

Community
  • 1
  • 1
Framester
  • 33,341
  • 51
  • 130
  • 192

1 Answers1

0
with q(str) as (
    values ('abc'), ('a-b'), ('a''b'), ('a&b'), ('a\b'))

select str, str ~ '[''\-&\\]' bool
from q;

 str | bool 
-----+------
 abc | f
 a-b | t
 a'b | t
 a&b | t
 a\b | t
(5 rows)
klin
  • 112,967
  • 15
  • 204
  • 232
  • Only one addition: Because of the use of sqlalchemy there has to be another '\' : `'[''\-&\\\]'` worked for me in the end as part of the python string. – Framester Aug 04 '16 at 08:51