在MySQL中使用正则表达式查找包含a-z、A-Z和0-9的字符串


要查找包含a-z、A-Z和0-9的字符串,请使用BINARY REGEXP以及AND运算符。

让我们先创建一个表:

mysql> create table DemoTable738 (UserId varchar(100));
Query OK, 0 rows affected (0.81 sec)

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

mysql> insert into DemoTable738 values('John');
Query OK, 1 row affected (0.16 sec)
mysql> insert into DemoTable738 values('sAm456');
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable738 values('98Carol');
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable738 values('67david');
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable738 values('69MIKE');
Query OK, 1 row affected (0.18 sec)

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

mysql> select *from DemoTable738;

这将产生以下输出:

+---------+
| UserId  |
+---------+
| John    |
| sAm456  |
| 98Carol |
| 67david |
| 69MIKE  |
+---------+
5 rows in set (0.00 sec)

以下是使用REGEXP查找字符串的查询:

mysql> select *from DemoTable738 where UserId REGEXP BINARY '[A-Z]' 
   and UserId REGEXP BINARY '[a-z]' and UserId REGEXP BINARY '[0-9]';

这将产生以下输出:

+---------+
| UserId  |
+---------+
| sAm456  |
| 98Carol |
+---------+
2 rows in set (0.03 sec)

更新于:2019年8月22日

439 次浏览

开启你的职业生涯

完成课程获得认证

开始学习
广告
© . All rights reserved.