简体   繁体   English

MySQL / Regexp:部分正则表达式匹配

[英]MySQL/Regexp: Partial regexp match

I have a bunch of regular expressions in a MySQL table. 我在MySQL表中有一堆正则表达式。 I want to know whether a given string matches a part of any regular expression or not. 我想知道给定的字符串是否匹配任何正则表达式的一部分。

Eg: 例如:

+----+--------------------------------+
| id | regexps                        |
+----+--------------------------------+
| 1  | foo-[0-9]*\.example\.com       |
| 2  | (bar|tux)-[0-9]*\.example\.com |
+----+--------------------------------+

(The regexps attribute is of VARCHAR type) regexps属性为VARCHAR类型)

foo-11.example.com matches the first regexp. foo-11.example.com与第一个正则表达式匹配。

I want a MySQL query that returns the first row with the given string as foo-11 我想要一个MySQL查询,该查询返回具有给定字符串foo-11的第一行

This should do it on MySql: 这应该在MySql上执行:

select * from table t where 'foo-11.example.com' rlike t.data;

There are other ways in PostGreSQL. PostGreSQL中还有其他方法。 Here's the link from where I have referenced this: 这是我引用的链接:

  1. http://www.tutorialspoint.com/mysql/mysql-regexps.htm http://www.tutorialspoint.com/mysql/mysql-regexps.htm
  2. Match a Query to a Regular Expression in SQL? 将查询匹配到SQL中的正则表达式?

PS: Using * is tricky though! PS:虽然使用*有点棘手!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM