regex in SQL to detect one or more digit

女生的网名这么多〃 提交于 2019-12-01 20:38:39

问题


I have the following query:

SELECT * 
FROM  `shop` 
WHERE  `name` LIKE  '%[0-9]+ store%'

I wanted to match strings that says '129387 store', but the above regex doesn't work. Why is that?


回答1:


Use REGEXP operator instead of LIKE operator

Try this:

SELECT '129387 store' REGEXP '^[0-9]* store$';

SELECT * FROM shop WHERE `name` REGEXP '^[0-9]+ store$';

Check the SQL FIDDLE DEMO

OUTPUT

|         NAME |
|--------------|
| 129387 store |



回答2:


If you mean MySQL, LIKE does not implement regular expressions. It implements the much more restricted SQL pattern matching, which just has two special operators: % matches any sequence of characters, and _ matches any single character.

If you want regular expression matching, you must use the REGEXP or RLIKE operator:

SELECT *
FROM shop
WHERE name REGEXP '[0-9]+ store'

MySQL's regular expression language doesn't include \d to specify digits, but you could write that as:

SELECT *
FROM shop
WHERE name REGEXP '[[:digit:]]+ store'

If the store name must begin with digits, you need an anchor:

SELECT *
FROM shop
WHERE name REGEXP '^[0-9]+ store'

You can learn more about regular expression syntax at regular-expressions.info.




回答3:


Try

SELECT * 
FROM  `shop` 
WHERE  `name` LIKE  '%[0-9] store%'


来源:https://stackoverflow.com/questions/20794860/regex-in-sql-to-detect-one-or-more-digit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!