MySQL RegEx to find lines containing N semi-colons?


Let us first create a table −

mysql> create table DemoTable
(
   Id int NOT NULL AUTO_INCREMENT PRIMARY KEY ,
   Title text
);
Query OK, 0 rows affected (0.88 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable(Title) values('This is; a; MySQL;Tutorial');
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable(Title) values('Java is; an;Object Oriented');
Query OK, 1 row affected (0.16 sec)
mysql> insert into DemoTable(Title) values('MongoDB ; is;a; database');
Query OK, 1 row affected (0.12 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+----+-----------------------------+
| Id | Title                       |
+----+-----------------------------+
|  1 | This is; a; MySQL;Tutorial  |
|  2 | Java is; an;Object Oriented |
|  3 | MongoDB ; is;a; database    |
+----+-----------------------------+
3 rows in set (0.00 sec)

Here is the regex to find lines containing 3 semi-colons −

mysql> select *from DemoTable where Title REGEXP '(.*;){3}';

This will produce the following output −

+----+----------------------------+
| Id | Title                      |
+----+----------------------------+
|  1 | This is; a; MySQL;Tutorial |
|  3 | MongoDB ; is;a; database   |
+----+----------------------------+
2 rows in set (0.00 sec)
raja
Published on 09-Oct-2019 14:44:38
Advertisements