[英]Find regex pattern by string in opposite way
I have a bunch of regex patterns stored in a database and I have a string as an input.我有一堆正则表达式模式存储在数据库中,我有一个字符串作为输入。 My goal is to find exact pattern that matches given string.我的目标是找到与给定字符串匹配的确切模式。 So it is kinda opposite to what regex is usually used for.所以它与正则表达式通常用于的有点相反。 Currently I am taking all the patterns from DB and check them one by one if it matches received string.目前,我正在从数据库中获取所有模式,如果与接收到的字符串匹配,则一一检查它们。 I was wondering whether it is possible to find match in SQL我想知道是否可以在 SQL 中找到匹配项
For example:例如:
input string:输入字符串:
'/user/profile/AxuI1dn/messages'
regex array in 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}$'
...
expected result here is这里的预期结果是
'^/user/profile/[A-Za-z0-9]{5,7}/messages$'
You can do this by simply using string REGEXP regexcolumn
.您可以通过简单地使用string REGEXP regexcolumn
来做到这string REGEXP regexcolumn
。 For example:例如:
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
Output:输出:
id regex
3 ^/user/profile/[A-Za-z0-9]{5,7}/messages$
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.