简体   繁体   中英

Matching special characters

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM