调整正则表达式以与MySQL一起使用 [英] Adapting a Regex to work with MySQL

查看:111
本文介绍了调整正则表达式以与MySQL一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我拥有的这个Regex在所有测试中都可以正常工作,但是尽管我一直在使用Google谷歌搜索并尝试其变形示例,但我无法使其在MySQL中正常工作.

I've got this Regex that works fine in all my testing, but I am unable to make it work in MySQL, despite the examples that I've been googling and trying variations of this:

^[A-Za-z]{2,4}-\d{3}-\d{2}$

在Javascript中,它可以正确匹配AA-001-01ZZZZ-999-99,因此在MySQL中尝试了对正则表达式进行各种调整,但都没有运气:

In Javascript, it correctly matches AA-001-01 through ZZZZ-999-99, and so have tried it in MySQL with various tweaks to the regex, all with no luck:

SELECT * FROM products WHERE sku REGEXP '^[A-Za-z]{2,4}-\d{3}-\d{2}$'

(该表包含数千条记录,其中sku与上面给出的示例匹配)

(The table contains thousands of records where sku matches the samples given above)

据我所知,对Mysql Regex的支持是有限的,但是这应该不能正常工作,或者在使用Mysql时还需要考虑其他语法吗?

As best as i can tell, Mysql Regex support is limited, but should this not work properly, or is there some further syntax consideration to work with Mysql?

推荐答案

要匹配数字,应使用[0-9][[:digit:]].

To match a digit you should use either [0-9] or [[:digit:]].

尝试一下:

SELECT col1, col2, ..., coln
FROM products
WHERE sku REGEXP '^[A-Za-z]{2,4}-[0-9]{3}-[0-9]{2}$'

查看它是否可以在线运行: sqlfiddle .

See it working online: sqlfiddle.

请参见 REGEXP 的手册.

See the manual for REGEXP.

这篇关于调整正则表达式以与MySQL一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆