Match optional end of line after every record with REGEXP?


Let us first create a table −

mysql> create table DemoTable(EmployeeCode varchar(100));
Query OK, 0 rows affected (0.56 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values('EMPLOYEE:100 John Smith');
Query OK, 1 row affected (0.18 sec)
mysql> insert into DemoTable values('EMPLOYEE:16537 Chris Brown');
Query OK, 1 row affected (0.10 sec)
mysql> insert into DemoTable values('EMPLOYEE:100 David Miller');
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable values('EMPLOYEE:100 23432 David Miller');
Query OK, 1 row affected (0.20 sec)

Display all records from the table using select statement −

mysql>; select *from DemoTable;

This will produce the following output −

+---------------------------------+
| EmployeeCode                    |
+---------------------------------+
| EMPLOYEE:100 John Smith         |
| EMPLOYEE:16537 Chris Brown      |
| EMPLOYEE:100 David Miller       |
| EMPLOYEE:100 23432 David Miller |
+---------------------------------+
4 rows in set (0.00 sec)

Following is the query to match optional end of line, for example, 23434 after 100 in “EMPLOYEE:100 23432 David Miller” −

mysql> select *from DemoTable where EmployeeCode REGEXP "EMPLOYEE:100([^0-9]|$)";

This will produce the following output −

+---------------------------------+
| EmployeeCode                    |
+---------------------------------+
| EMPLOYEE:100 John Smith         |
| EMPLOYEE:100 David Miller       |
| EMPLOYEE:100 23432 David Miller |
+---------------------------------+
3 rows in set (0.00 sec)

Updated on: 22-Aug-2019

31 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements