简体   繁体   English

匹配特殊字符

[英]Matching special characters

I want to match a list of special characters ( '-& ) for a postgresql query. 我想匹配一个特殊字符列表( '-& )的PostgreSQL查询。 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 结果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. 我找到了其他答案,例如https://stackoverflow.com/a/25925429/380038 ,但这些都匹配所有特殊字符。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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