Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 如何在Postgres中使用模式匹配_Sql_Regex_Postgresql_Select - Fatal编程技术网

Sql 如何在Postgres中使用模式匹配

Sql 如何在Postgres中使用模式匹配,sql,regex,postgresql,select,Sql,Regex,Postgresql,Select,我有一个基本的图书目录,如下所示: CREATE TABLE books ( id integer primary key, name text not null ); INSERT INTO books (id, name) VALUES (1, 'The Ghost'); 我想搜索与搜索词^Ghost$匹配的图书名称。正如你所看到的,这个术语中有一些正则表达式。如何通过正则表达式进行匹配 我试着这么做,但没有结果 select * from books WHERE name lik

我有一个基本的图书目录,如下所示:

CREATE TABLE books (
  id integer primary key,
  name text not null
);
INSERT INTO books (id, name) VALUES (1, 'The Ghost');
我想搜索与搜索词
^Ghost$
匹配的图书名称。正如你所看到的,这个术语中有一些正则表达式。如何通过正则表达式进行匹配

我试着这么做,但没有结果

select *
from books
WHERE name like '%^The Ghost$%'

SQL fiddle:

如果您的需求确实是逐字匹配的
^Ghost$
,那么您可以在此处使用相等比较:

选择*
从书本上
其中name=‘幽灵’;
如果要使用正则表达式表达上述内容,可以使用
~
运算符:

选择*
从书本上
其中name~“^theghost$”;

对于没有任何正则表达式的术语,可以使用
~
来代替
之类的
?它还能和部分书名匹配吗?回答:也许吧。它取决于regex/like表达式。将
名称命名为“%theghost%”
name~“theghost”相同吗?
是的,在本例中,这两个表达式将匹配相同的内容。