Oracle regular expressions: search by pattern exact match

Here you can learn about finding the exact regular expression match using SQL query in Oracle. Article is made upon examples so you will be on the right track in no time.

Usually when we want to search Oracle database users table via regular expressions by email domain filter, it's easy:

WHERE regexp_like(mail, '@gmail\.com');

the above query will return all users which have an email in domain.

But lets say that we want to be more specific and we want to search the database for users whose name is John and last name is starting with "B". For this example, lets assume that some users email address format is "".

If we use this statement:

WHERE regexp_like(mail, 'john\.b[a-zA-Z]*@gmail\.com');

some of the results will be what we want but a lot will be just a trash pile. This statement will return users with emails like "" or "". Why? Because regexp_like function checks if the given string contains a substring that matches our regular expression and NOT if the entire string matches the regular expression.

The solution to this problem i very simple. We must add two special characters. One, ^ (caret), at the beginning and the other, $ (dollar), at the end of our regular expression.

Quick explanation:
^ (caret) - means that the string starts with regular expression pattern.
$ (dollar) - means that the string ends with regular expression pattern.

Knowing this correct statement should look like this:

WHERE regexp_like(mail, '^john\.b[a-zA-Z]*@gmail\.com$');


Post new comment

  • Lines and paragraphs are automatically recognized. The <br /> line break, <p> paragraph and </p> close paragraph tags are inserted automatically. If paragraphs are not recognized simply add a couple blank lines.

More information about formatting options

This question is for testing whether you are a human visitor and to prevent automated spam submissions.
5 + 1 =
This simple math problem is designed for BOT - you just have to enter '7'.