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

mysqlmysqli database更新于 2025/10/4 16:44:17

要查找包含 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 在表中插入一些记录 command  −

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)

相关文章


有用资源