MariaDB - Regular Expression
Last Updated :
23 Jan, 2024
MariaDB is also a relational database language that is similar to SQL. However, the introduction of MariaDB took place as it is an extension to SQL and contains some more advanced operators rather than SQL. MariaDB contains operators similar to SQL like CRUD operations and between operators and other operators like Limit and in such a way it also provides support to extract data from the database with the help of regular expressions. MariaDB is fast, scalable, and robust also it can be part of the database infrastructure for Big Data.
In this article, we will learn about the REGEX in MariaDB in detail along with its syntax, importance, practical implementations of examples, and so on.
Regular Expressions in MariaDB
Regular expressions are used to match the pattern given in a particular string. The regular expressions are mainly used to make the data extraction easier based on some conditions. In SQL language, we are aware of the LIKE operator which is used along with the % and _ characters to match the pattern after the specified condition.
In such a way we are aware of some of the regular expressions used in the programming languages such as Python and Java.
- In Python to work with regular expressions we will import the re-module to work with the regular expression.
- In Java, we import the java.util.regex package to work with the regular expressions.
- In the case of MariaDB, we use two clauses to match the regular expressions such as the similarLIKE clause and the REGEXP clause in MariaDB similar to SQL.
We use the regular expressions in the databases to extract the table records more easily. Instead of writing multiple conditions in the databases, we use regular expressions to extract the queries more easily and satisfy the conditions too.
Syntax for using REGEXP in MariaDB:
SELECT column_name FROM table_name WHERE column_name REGEXP 'pattern' ;
SELECT * FROM table_name WHERE column_name REGEXP 'pattern' ;
Generally, what are the patterns that are used? A pattern that is used in any language can be the combination of the below values.
Some Regular Expressions
|
*
| The * character matches Zero or more occurrences of the specified alphabet or digit before *.
|
+
| The + character matches One or more occurrences of the specified digit or alphabet before +.
|
?
| The ? character matches one or more occurrences of the specified pattern that was mentioned before.
|
|
| Just like the or symbol operator in programming languages. It matches any of the expression either before or after the | symbol,
|
[ ]
| The symbol matches the characters that are specified only within the two square brackets.
|
[ ^ ]
| The symbol matches the characters except the characters that are specified after the ^ symbol in the square brackets.
|
.
| It Matches any character except null.
|
{ m }
| It Matches the characters m times. Suppose if the m value was 2. It only matches the pattern two times even if it is repeated more than 2.
|
$
| Generally it is used to match a specific pattern at the end of the string .
|
^
| The ^ character is used to match the given pattern at the beginning of the string.
|
{m, }
| The mentioned pattern matches characters in the string atleast m times .
|
{m,n}
| The mentioned value matches atleast m times and atmost n times. It means it matches upto 8 characters in the string and greater than that .
|
\d
| It only matches the numeric characters.
|
( )
| It mainly matches the sub expression in the regular expression.
|
\w
| It matches only alphabetic characters in the given string.
|
\S
| It Matches white space characters in the pattern.
|
Example of Regular Expressions in MariaDB
To understand the Regex in the MariaDB we need a table on which we will perform various operations. So in this article we have a table called students which consist of id, name, subject, and GMAIL as Columns. After Inserting some data into the students table the table looks:
OutputExplanation: In the above image we have retrieved the data from the students.
Example 1: Extracting Records from the Students Table Whose Name Starts with Only Vowels
In this query we are going to extract the records from the students table whose name starts with only vowels.
Query:
SELECT * FROM students WHERE name REGEXP '^[AEIOUaeiou][a-zA-Z]+';
Output:
Names start with vowelsExplanation:
- In the above image we have extracted name from the students table whose names starts with the vowels in the table.
- In the pattern we have first specified ^ it matches the beginning only the letters that starts with vowels.
- Next we have used [aeiouAEIOU] so that it matches the first letter vowel which may be either uppercase or lowercase.
- In the next we have specified [a-zA-Z]+ as we already know the + symbol matches one or more occurrences in the given
string. It matches the string which contains only alphabets. Because name of any person contains only alphabets.
Example 2: Extracting Records from students Table Whose Name Starts With Consonant and Ends With the Vowels
In this query we are going to extract the records from the relation name students whose name starts with consonant and ends with vowel.
Query:
SELECT * FROM students WHERE name REGEXP '^[^AEIOUaeiou][a-zA-Z]+ ?[a-zA-Z]+[aeiouAEIOU]$' ;
Output:
names starts with vowels and consonantsExplanation:
- In the above image we have extracted all the records whose name starts with consonants and ends with the vowel.
- The ^ character is used to match the consonants at the starting of the string.
- The [^aeiouAEIOU] pattern matches all the characters except that were mentioned in the square brackets.
- Then after we have used [a-zA-Z]+ which matches all the words from one or more occurences.
- Then we have used " "? which matches either zero or more occurences.It is because if the name contains firstname and lastname there will be a space between them.To match the whole name we have used that pattern.
- The [aeiouAEIOU]$ matches the vowels at the end of the string.
Example 3: Extracting Records from Student Table Whose Name has Length More than or Equal to 8.
In this query we are going to extract the records from the students table whose name is of length greater than or equal to 8.
Query:
SELECT * FROM students WHERE name REGEXP '[a-zA-Z]{8, }';
Output:
Names >=8Explanation:
- In the above image we are extracting the records whose names length are greater than 8.
- In the above image we have used [a-A-Z " "] to match all the alphabets that are present in the column name.We have also included the white space along with the alphabets to even match the space in the name.
- After that we have used { 8, } which matches atleast 8 alphabets present in the name column.
- It matches either 8 or more alphabets in the name.
Note: In the above pattern we have [a-zA-Z " "] there was a white space to match the words that have space between them.
Example 4: Extracting Records from Student Table in which Email Starts with Alphabets and Contains Only Alphabets and Numbers Followed by Domain @gmail.com
In this we are going to extract the records from the students table where the email consists of only alphabets and numbers which ends with the domain name @gmail.com.
Query:
SELECT * FROM students WHERE GMAIL REGEXP '^[A-Za-z][a-zA-Z]+[0-9][email protected]' ;
Output:
names with @gmail.com along with alphabets and numbers.Explanation:
- In the above image we are extracting the email that consists of only starting with the alphabets followed by digits.
- The user name is to be followed by the domain name @gmail.com
Example 5: Extracting Records from Student Table Whose Name is Exactly of Length of Characters 5
In the query we are going to extract the records from the student table whose name is exactly of length 5.
Query:
SELECT * FROM students WHERE name REGEXP "^[A-Za-z]{5}$" ;
Output:
NAMES WITH LENGTH 5Explnation:
- In the above image we have used the regular expressions to extract the name which is of exactly length 5 .
- The ^ character is used to match the pattern at the beginning of the string.
- The [a-zA-Z] is used to match only the alphabets in the string.
- The ^ and $ are used to match the alphabets at the starting and ending of the string.
Explanation 6: Extracting Records From the Table Where Name Ends With "a" or "A"
In this query we are going to extract the records from the students whose name ends with the a or A.
Query:
SELECT * FROM students WHERE name REGEXP "^[A-Za-z][a-zA-Z ]+[Aa]$";
Output:
OUTPUT NAMES END WITH A or aExplanation:
- In the above image first the name should be started with the alphabet so we have used the pattern ^[a-A-Z].
- In the above image after a alphabet is matched then the words are matched along with " " because we have mentioned the white space in [a-zA-Z " "]+ or \S in words.
- After that [aA]$ which matches either a or A at the end of the string.
- In this way we have matched the string that ends with the a or A.
Conclusion
The regular expressions shortly known as REGEX, they are too powerful which can easily perform the data pre-processing tasks in a easier manner. They are used to identify and parsing the data present in the relations [tables]. They are used to replace the data and also helpful in managing and manipulating the data in the database. The regular expressions are also used in many database related languages such as SQL , MySQL and even in non relational database languages such as MongoDB [ $regex operator ]. Regex queries can be optimized for performance even with large datasets.
Similar Reads
Regular Expression Matching
Given a text t and a pattern p where t consists of only lowercase English alphabets while p consists of lowercase English alphabets as well as special characters '.' and '*', the task is to implement a function to test regular expression such that: '.' Matches any single character.ââââ'*' Matches ze
14 min read
Regular Expressions In R
Regular expressions (regex) are powerful tools used in programming languages like R for pattern matching within text data. They enable us to search for specific patterns, extract information, and manipulate strings efficiently. Here, we'll explore the fundamentals of regular expressions in R Program
5 min read
Kotlin Regular Expression
Regular expressions (regex) are powerful tools used for pattern matching and text manipulation. They are fundamental in almost every modern programming language, and Kotlin is no exception. In Kotlin, regular expression support is provided through the Regex class. An instance of this class represent
4 min read
Regular Expressions in SQL
Regular expressions (regex) are incredibly powerful tools that simplify complex data processing tasks. They enable users to identify, replace, or extract data based on specific patterns. Regular expressions, though originating from computer science, are widely used in SQL to manage and manipulate da
7 min read
Regular Expressions in Scala
Regular Expressions explain a common pattern utilized to match a series of input data so, it is helpful in Pattern Matching in numerous programming languages. In Scala Regular Expressions are generally termed as Scala Regex. Regex is a class which is imported from the package scala.util.matching.Reg
5 min read
Ruby | Regular Expressions
A regular expression is a sequence of characters that define a search pattern, mainly for use in pattern matching with strings. Ruby regular expressions i.e. Ruby regex for short, helps us to find particular patterns inside a string. Two uses of ruby regex are Validation and Parsing. Ruby regex can
3 min read
MySQL | Regular Expressions (Regexp)
In MySQL, regular expressions (REGEX) offer powerful functionality for flexible pattern matching within string data. By using the REGEXP and RLIKE operators, developers can efficiently search, validate, and manipulate string data in more dynamic ways than simple LIKE queries. In this article, we wil
7 min read
Properties of Regular Expressions
Regular expressions, often called regex or regexp, are a powerful tool used to search, match, and manipulate text. They are essentially patterns made up of characters and symbols that allow you to define a search pattern for text. In this article, we will see the basic properties of regular expressi
7 min read
Posix Basic Regular Expressions
POSIX stands for Portable Operating System Interface. It defines a set of standard operating system interfaces based on the UNIX OS. These standards are specified by the IEEE (Institute of Electrical and Electronics Engineers) society which maintains the compatibility between different operating sys
4 min read
Perl | Operators in Regular Expression
Prerequisite: Perl | Regular Expressions The Regular Expression is a string which is the combination of different characters that provides matching of the text strings. A regular expression can also be referred to as regex or regexp. The basic method for applying a regular expression is to use of bi
4 min read