REGULAR EXPRESSION IN MYSQL
Improve your Pattern Matching with these Queries
REGULAR EXPRESSION:
Regular Expressions are sequence/pattern of characters that is used to define a search pattern mainly used in string searching algorithms and pattern matching in programming languages like C++ and python.
Pattern matching is also extensively used in SQL queries to find the data that’s present in certain pattern.
This article attempts to explain all the regular expression in MySQL and execute the same in Employees Database and Olympics Database.
^ :
Matches the beginning of the string.
and the important thing to note is regexp in MySQL is not case sensitive so the query below gives same output as the above one.
$ :
Matches the end of string.
. or .{} :
Dot character matches any single character except for a new line and can be used to define the number of characters you are expecting a string to have like,
This query uses ^ and $ match the beginning and ending of last_name and five ‘.’ to define 5 characters
As you can see the query returns records that has only 5 characters in last_name.
This query can also be written like
.{n} can be used instead of repeating (.) n times
[ ] :
using square brackets to optimize the pattern matching even more.
Records containing specific characters in first_name:
Records that has first_name starting with specific characters:
Records that has first_name ending with specific characters:
CONCLUSION:
This article contains some of the basic regexp patterns, hope you find this article helpful.
Thank you……….