where 子句中的 mySQL 正则表达式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3452572/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 16:47:53  来源:igfitidea点击:

mySQL regex in the where clause

mysqlregex

提问by Matt Elhotiby

SELECT telephone_number
FROM table
WHERE telephone_number REGEXP '^1[() -]*999[() -]*999[() -]*9999$';

how do i make so its valid for any number format and any number like

我如何使它对任何数字格式和任何数字都有效

407-888-0909
1(408)998-7654
7776654433
876-7788

right now its only valid for 1-999-999-9999

现在它只对 1-999-999-9999 有效

回答by OMG Ponies

Use:

用:

SELECT telephone_number
  FROM table
 WHERE telephone_number REGEXP '^1[() -]*[[:digit:]]{3}[() -]*[[:digit:]]{3}[() -]*[[:digit:]]{4}$';

Reference:

参考:

回答by dockeryZ

It isn't very wise to store phone numbers in a database with spaces, dashes, parentheses, etc. The most efficient way is to truncate all that garbage to a simple 10 digit number. That way you can actually store the number in an INTEGER based column instead of a VARCHAR.

用空格、破折号、括号等将电话号码存储在数据库中不是很明智。最有效的方法是将所有这些垃圾截断为一个简单的 10 位数字。这样,您实际上可以将数字存储在基于 INTEGER 的列而不是 VARCHAR 中。

回答by jacob YO

SELECT telephone_number
  FROM table
 WHERE telephone_number REGEXP '[1]?[(]?[[:DIGIT:]]{3}[)]?[-]?[[:DIGIT:]]{3}[-]?[[:DIGIT:]]{4}'