在Postgres中的WHERE中使用正则表达式

Dav*_*vid 69 regex postgresql

我目前有以下查询:

select regexp_matches(name, 'foo') from table;
Run Code Online (Sandbox Code Playgroud)

我怎样才能重写这个,以便正则表达式在下面的地方(不工作):

select * from table where regexp_matches(name, 'foo');
Run Code Online (Sandbox Code Playgroud)

当前的错误消息是:错误:WHERE的参数必须是boolean类型,而不是type text [] SQL状态:42804字符:29

ara*_*nid 122

改为写:

select * from table where name ~ 'foo'
Run Code Online (Sandbox Code Playgroud)

'〜'运算符产生一个布尔结果,表示正则表达式是否匹配,而不是提取匹配的子组.

  • 有没有〜运算符的替代方案?我正在使用一个解析查询的javascript库,不幸的是,它无法识别〜及其任何变体. (2认同)

Max*_*yko 42

在那里使用匹配运算符:

select * from table where name ~ 'foo';
Run Code Online (Sandbox Code Playgroud)