调整正则表达式以与MySQL一起使用

编程入门 行业动态 更新时间:2024-10-25 15:19:22
本文介绍了调整正则表达式以与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-01至ZZZZ-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一起使用

本文发布于:2023-10-17 13:17:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1501030.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:正则表达式   MySQL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!