【问题标题】:PostgreSQL reverse pattern matching (pattern in field)PostgreSQL 反向模式匹配(字段中的模式)
【发布时间】:2014-12-04 17:09:22
【问题描述】:

我有一张这样的桌子:

id: 1
pattern: /test.*

id: 2
pattern: /hello-world

这个输入值:/test-request

现在我想为给定的输入创建一个查询,postgres 应该返回第二列中的模式与输入匹配的行(正则表达式搜索,但正则表达式在要搜索的字段中)

postgres 数据库可以做到这一点吗?

【问题讨论】:

    标签: regex postgresql


    【解决方案1】:

    您可以为正则表达式运算符使用列值:

    select id, pattern 
    from patterns
    where '/test-request' ~* pattern;
    

    ~* 进行不区分大小写的匹配,~ 进行区分大小写的匹配。

    SQLFiddle 示例:http://sqlfiddle.com/#!15/388a4/2

    【讨论】:

    • 不知道可以改变两个字段的顺序,谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-25
    • 1970-01-01
    • 1970-01-01
    • 2021-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多