Regular Expression Guide
Regular Expression Guide.
Last updated:
Warning: Review and test in a non-production environment before running.
-- Operator reference based on the Oracle Database documentation, "Using Regular Expressions in Database Applications" – https://docs.oracle.com/en/database/oracle/oracle-database/19/adfns/regexp.html
REGEXP_LIKE
Searches a character column for a pattern.
Use this function in the WHERE clause of a query to return rows matching a regular expression.
The condition is also valid in a constraint or as a PL/SQL function returning a boolean.
The following WHERE clause filters employees with a first name of Steven or Stephen:
WHERE REGEXP_LIKE(first_name, '^Ste(v|ph)en$')
REGEXP_REPLACE
Searches for a pattern in a character column and replaces each occurrence of that pattern with the specified string.
The following function puts a space after each character in the country_name column:
REGEXP_REPLACE(country_name, '(.)', '\1 ')
REGEXP_INSTR
Searches a string for a given occurrence of a regular expression pattern and returns an integer
indicating the position in the string where the match is found.
You specify which occurrence you want to find and the start position.
For example, the following performs a boolean test for a valid email address in the email column:
REGEXP_INSTR(email, '\w+@\w+(\.\w+)+') > 0
REGEXP_SUBSTR
Returns the substring matching the regular expression pattern that you specify.
The following function uses the x flag to match the first string by ignoring spaces in the regular expression:
REGEXP_SUBSTR('oracle', 'o r a c l e', 1, 1, 'x')
REGEXP_COUNT
Metacharacter Syntax Operator Name Description
-------------------- --------------------------------------- --------------------------------------------------------------
. Any Character -- Dot Matches any character
+ One or More -- Plus Quantifier Matches one or more occurrences of the preceding subexpression
? Zero or One -- Question Mark Quantifier Matches zero or one occurrence of the preceding subexpression
* Zero or More -- Star Quantifier Matches zero or more occurrences of the preceding subexpression
{m} Interval--Exact Count Matches exactly occurrences of the preceding subexpression
{m,} Interval--At Least Count Matches at least m occurrences of the preceding subexpression
{m,n} Interval--Between Count Matches at least m, but not more than n occurrences of the preceding subexpression
[ ... ] Matching Character List Matches any character in list ...
[^ ... ] Non-Matching Character List Matches any character not in list ...
| Or 'a|b' matches character 'a' or 'b'.
( ... ) Subexpression or Grouping Treat expression ... as a unit.
The subexpression can be a string of literals or a complex
expression containing operators.
\n Backreference Matches the nth preceding subexpression, where n is an integer from 1 to 9.
\ Escape Character Treat the subsequent metacharacter in the expression as a literal.
^ Beginning of Line Anchor Match the subsequent expression only when it occurs at the beginning of a line.
$ End of Line Anchor Match the preceding expression only when it occurs at the end of a line.
[:class:] POSIX Character Class Match any character belonging to the specified character class.
Can be used inside any list expression.
[.element.] POSIX Collating Sequence Specifies a collating sequence to use in the regular expression.
The element you use must be a defined collating sequence, in the current locale.
[=character=] POSIX Character Equivalence Class Match characters having the same base character as the character you specify.
[:digit:] Any digit
[:alpha:] Any upper or lower case letter
[:lower:] Any lower case letter
[:upper:] Any upper case letter
[:alnum:] Any upper or lower case letter or number
[:xdigit:] Any hex digit
[:blank:] Whitespace
[:space:] Space, tab, return, line feed, form feed
[:cntrl:] Control Character, non printing
[:print:] Printable character including a space
[:graph:] Printable characters, excluding space.
[:punct:] Punctuation character, not a control character or alphanumeric
*/