Regular Expression in Oracle SQL
Regular Expression in Oracle SQL is a pattern matching technique to retrieve sort of data based on the conditions given in Oracle database.
Pattern Matching is similar to functions like, replace, substring and in string which are previously seen topics.
At the end of this article there will be certain task to practice for this Regular Expressions. It is very important to be very clear about this topic, this are very helpful for many interview Questions.
Using regular expressions with Oracle Database: Overview
Regular Expression is one of the easiest way to learn with clear understanding if one are familiar with the below
- Basic string matching patterns,
- Quantifiers,
- Meta characters,
- Functions etc.
Note: The above points should understand clearly means it will be very easy to write a query.
Basic String Matching with Regular Expressions
Literals of regular expression in oracle sql
Quantifiers used in Regular Expression of SQL
Meta characters in Regular Expression of SQL
Oracle Database SQL Functions for Regular Expressions
In Oracle Database SQL functions consists of 5 functions in regular expressions such as:-
- REGEXP_LIKE
- REGEXP_SUBSTR
- REGEXP_INSTR
- REGEXP_REPLACE
- REGEXP_COUNT
This are the five most important regex pattern matching expressions are going to see detail with examples and explanations
REGEXP_LIKE
Like operator is used to get the record for the giving pattern.
This Regexp_like can be used in Where condition not in SELECT clause.
Let's Understand some basic match parameters which are helps to get the record based on our input string.
End of the Studying Part.
If you completed reading the above tables or charts, is only enough to retrieve the query
From the above information, now we are going to workout all the patterns with examples
We will use this table for data retrieving.
Table Name : detail
Script for the above table
REGEXP_LIKE patterns with examples
Basic Syntax : REGEXP_LIKE (source string, pattern)
1, Print the Name which starts with letter 'k' it may be any cases (Upper or lower)
The above query only print Lower case "k" but not prints Upper case "K".
Similarly instead of Lower "k" if we give "K" means it will print upper case name only.
To print all (K and k) we need to use (i) which mentions case in sensitive matching.
2) Print the name which contains "a"
{1} it is a quantifiers used to mention the n times.
WHERE REGEXP_LIKE (NAME, '[a]{2}') => it is used to print two continuous "a"
Example: aarthy, raam, aakash, saami, etc..
Same as for three, four etc...
3) Print the name that must contain the letter (r, s, t, u, v, w, x, y, z)
[r-z] patterns used to print the letter from r to z of all characters (literals in regular expression).
4) Print the name that must contain all character such as "akr"
and write another query to print any of the name should contain this any one character "akr"
SELECT name FROM detail
WHERE REGEXP_LIKE (name, '^m[a-z]+n$');
^m used to define letter starts with m
[a-z] next letter will be between a to z
+ denotes 1 or more times
n
$ means ends
6) The name that should ends with vowels (a, e, i, o, u)
SELECT * FROM detail
WHERE REGEXP_LIKE (Name, '[a-z]+(a|e|i|o|u)$');
() - group
| - used to identify either or
$ - used to end the word
7) Find the Gmail user and yahoo user in the table
8) Print the ID and NAME that contains first letter should be between (a) to (z)
CONCLUSION
I hope you got some clear understanding about the basic patterns to regular expression in oracle SQL, and as well as regexp_like functions.
This is the first part of the regular expression functions and other 4 functions are explained in next 2 parts.
If anyone having any queries or doubts means kindly reach out us through official mail or comment below.
Comments
Post a Comment
Thanks for commenting, we will always respond your comment.