使用正则表达式匹配mysql中模式的子字符串

h9a6wy2h  于 2021-06-25  发布在  Mysql
关注(0)|答案(1)|浏览(325)

我想在mysql中查询所有匹配以下模式的字符串。
至少一个非空字符,
后跟文字破折号字符 - ,
然后后跟至少一个非空字符,
然后后跟文字字符串 in ('true') “的子字符串” and “不能出现在两者之间 - 以及 in ('true') .
例如:

segment-123 in ('true')

符合上述模式。

content-foo and segment in ('true')

与上面的模式不匹配,因为它有子字符串“ and “中间 - 以及 in ('true') .
在mysql中使用regexp可以实现吗?非常感谢您的帮助。

46scxncf

46scxncf1#

mysql> select 'segment-123 in (\'true\')'
    regexp '[^[:space:]]+-[^[:space:]]+ in \\(\'true\'\\)' as matched;
+---------+
| matched |
+---------+
|       1 |
+---------+

mysql> select 'content-foo and segment in (\'true\')' 
    regexp '[^[:space:]]+-[^[:space:]]+ in \\(\'true\'\\)' as matched;
+---------+
| matched |
+---------+
|       0 |
+---------+

看到了吗https://dev.mysql.com/doc/refman/8.0/en/regexp.html#regexp-有关mysql中正则表达式语法的更多文档。

相关问题