MySQL中的REGEX仅显示由连字符分隔的数字。

让我们首先创建一个表-

create table DemoTable
   (
      Code varchar(100)
   );

使用插入命令在表中插入一些记录-

insert into DemoTable values('100-677-9876');
insert into DemoTable values('100-677-9876-John');
insert into DemoTable values('David-100-677-9876');

使用select语句显示表中的所有记录-

select *from DemoTable;

这将产生以下输出-

+--------------------+
| Code               |
+--------------------+
| 100-677-9876       |
| 100-677-9876-John  |
| David-100-677-9876 |
+--------------------+
3 rows in set (0.00 sec)

以下是正则表达式以仅显示由连字符分隔的数字-

select *from DemoTable where Code REGEXP '^\\(?[0-9]{3}\\)?[\\s-]?[0-9]{3}[\\s-]?[0-9]{4}$';

这将产生以下输出-

+--------------+
| Code         |
+--------------+
| 100-677-9876 |
+--------------+
1 row in set (0.00 sec)