Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/61.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
Mysql 以相反的方式通过字符串查找正则表达式模式_Mysql_Sql_Regex_String - Fatal编程技术网

Mysql 以相反的方式通过字符串查找正则表达式模式

Mysql 以相反的方式通过字符串查找正则表达式模式,mysql,sql,regex,string,Mysql,Sql,Regex,String,我在数据库中存储了一堆正则表达式模式,并且有一个字符串作为输入。 我的目标是找到与给定字符串匹配的确切模式。 所以它有点与正则表达式通常的用途相反。 目前,我正在从数据库中获取所有模式,并逐一检查它们是否与接收到的字符串匹配。 我想知道是否有可能在SQL中找到匹配项 例如: create table regexes (id int auto_increment primary key, regex varchar(50)) ; insert into

我在数据库中存储了一堆正则表达式模式,并且有一个字符串作为输入。 我的目标是找到与给定字符串匹配的确切模式。 所以它有点与正则表达式通常的用途相反。 目前,我正在从数据库中获取所有模式,并逐一检查它们是否与接收到的字符串匹配。 我想知道是否有可能在SQL中找到匹配项

例如:

create table regexes (id int auto_increment primary key,
                      regex varchar(50))
;
insert into regexes (regex) values
    ('^/user/stories/[A-Za-z0-9]{5,7}$'),
    ('^/user/profile/[0-9]{1,5}/$'),
    ('^/user/profile/[A-Za-z0-9]{5,7}/messages$'),
    ('^/user/messages/([^\s]+)/$'),
    ('^/user/profile/[0-9]{1,5}/photos/[A-Za-z0-9]{5,7}$')
;
SELECT *
FROM regexes 
WHERE '/user/profile/AxuI1dn/messages' regexp regex
输入字符串:

'/user/profile/AxuI1dn/messages'
以DB为单位的正则表达式数组:

'^/user/stories/[A-Za-z0-9]{5,7}$'
'^/user/profile/[0-9]{1,5}/$'
'^/user/profile/[A-Za-z0-9]{5,7}/messages$'
'^/user/messages/([^\s]+)/$'
'^/user/profile/[0-9]{1,5}/photos/[A-Za-z0-9]{5,7}$'
...
这里的预期结果是

'^/user/profile/[A-Za-z0-9]{5,7}/messages$'

只需使用
字符串REGEXP regexcolumn
即可完成此操作。例如:

create table regexes (id int auto_increment primary key,
                      regex varchar(50))
;
insert into regexes (regex) values
    ('^/user/stories/[A-Za-z0-9]{5,7}$'),
    ('^/user/profile/[0-9]{1,5}/$'),
    ('^/user/profile/[A-Za-z0-9]{5,7}/messages$'),
    ('^/user/messages/([^\s]+)/$'),
    ('^/user/profile/[0-9]{1,5}/photos/[A-Za-z0-9]{5,7}$')
;
SELECT *
FROM regexes 
WHERE '/user/profile/AxuI1dn/messages' regexp regex
输出:

id  regex
3   ^/user/profile/[A-Za-z0-9]{5,7}/messages$

请用您正在使用的数据库标记您的问题:mysql、oracle、postgresql。。。?。另外,请以表格形式提供样本数据和所需结果。简短回答为“否”。即使有可能,测试每一项也不会慢于IMHO。