If you've ever pulled thousands of rows into an application just to check whether a product code looks right, this post is for you. Oracle's built-in regular expression functions let you validate, search and tidy text right where the data lives, so you don't need extra string-handling code in every app that touches the table.
The toolkit in one glance
Oracle gives you one condition and four functions:
| Name | What you get back |
|---|---|
REGEXP_LIKE | A true/false test, used in WHERE clauses and constraints |
REGEXP_COUNT | How many times a pattern shows up |
REGEXP_INSTR | The position where a match starts (or ends) |
REGEXP_SUBSTR | The matching text itself |
REGEXP_REPLACE | The string with matches swapped for something else |
Each accepts an optional match-parameter string. The useful flags are i (ignore case), c (respect case), n (let the dot match a newline), m (treat each line as its own line for anchors) and x (ignore spaces in the pattern).
Setting up the sample data
CREATE TABLE stock_items (
sku VARCHAR2(20),
item_desc VARCHAR2(100),
supplier_email VARCHAR2(80),
delivery_date VARCHAR2(10),
handling_note VARCHAR2(200)
);
INSERT INTO stock_items VALUES
('BRK-0215', ' Heavy duty brake rotor 330mm ', 'accounts@brakeworks.example.com.au', '07/03/2026',
'<note>fragile</note><note>keep dry</note>');
INSERT INTO stock_items VALUES
('ROT-1180B', 'Rotor rotor bolt kit', 'sales@boltbarn.example.com.au', '19/11/2026',
'<note>pallet only</note>');
COMMIT;
1. Guarding the door with a CHECK constraint
Say every SKU must be three capital letters, a hyphen, four digits, and optionally one trailing capital letter. One constraint handles it for every application:
ALTER TABLE stock_items ADD CONSTRAINT ck_sku_shape
CHECK (REGEXP_LIKE(sku, '^[A-Z]{3}-[0-9]{4}[A-Z]?$', 'c'));
The anchors ^ and $ force the whole value to fit the pattern, and the c flag keeps lowercase letters out. Results:
BRK-0215 accepted ROT-1180B accepted brk-0215 rejected (lowercase) BRK0215 rejected (no hyphen) BRK-215 rejected (only three digits) BRK-0215BB rejected (two trailing letters)
2. Counting words
SELECT item_desc,
REGEXP_COUNT(item_desc, '\w+') AS word_count
FROM stock_items;
For 'Rotor rotor bolt kit' this returns 4. It's a quick way to flag descriptions that are too short to be useful.
3. Pulling out a piece of text
To get each supplier's domain, capture it in a group and ask for that group with the sixth argument:
SELECT REGEXP_SUBSTR(supplier_email, '@(\S+)$', 1, 1, NULL, 1) AS supplier_domain FROM stock_items;
brakeworks.example.com.au boltbarn.example.com.au
The @ is matched but left out of the result, because only the bracketed group is returned.
4. Finding where something sits
SELECT REGEXP_INSTR('Rotor kit 330mm', '\d') AS first_digit,
REGEXP_INSTR('Rotor kit 330mm', '\d+', 1, 1, 1) AS just_after_number
FROM dual;
FIRST_DIGIT JUST_AFTER_NUMBER
----------- -----------------
11 14
The fifth argument, 1, asks for the position immediately after the match instead of its start.
5. Cleaning up messy whitespace
Our first row has leading, trailing and doubled spaces:
SELECT REGEXP_REPLACE(TRIM(item_desc), '\s{2,}', ' ') AS tidy_desc
FROM stock_items;
' Heavy duty brake rotor 330mm ' becomes 'Heavy duty brake rotor 330mm'. Any run of two or more whitespace characters collapses into one space.
6. Back references: reshuffling and spotting repeats
A back reference lets you reuse what a group captured. Here we convert Australian-style dates (DD/MM/YYYY) to ISO format:
SELECT delivery_date,
REGEXP_REPLACE(delivery_date,
'^(\d{2})/(\d{2})/(\d{4})$',
'\3-\2-\1') AS iso_date
FROM stock_items;
07/03/2026 2026-03-07 19/11/2026 2026-11-19
Rows that don't fit the pattern pass through unchanged, so test for that before trusting the output.
Back references also catch accidental repeats, such as "Rotor rotor":
SELECT sku FROM stock_items WHERE REGEXP_LIKE(item_desc, '(^| )(\w+) \2( |$)', 'i');
Group 2 captures a word, and \2 demands the same word straight after it. This flags ROT-1180B.
7. Greedy versus lazy matching
By default, quantifiers grab as much as they can. Adding ? makes them take as little as possible:
SELECT REGEXP_SUBSTR(handling_note, '<note>.*</note>') AS greedy,
REGEXP_SUBSTR(handling_note, '<note>.*?</note>') AS lazy
FROM stock_items
WHERE sku = 'BRK-0215';
GREEDY LAZY ---------------------------------------------- --------------------- <note>fragile</note><note>keep dry</note> <note>fragile</note>
Greedy runs to the last closing tag, while lazy stops at the first.
Gotchas worth remembering
- Character classes need two sets of brackets. Write
[[:upper:]]+, not[:upper:]+. The inner pair names the class and the outer pair makes it a list. - Don't assume Perl, Python or JavaScript syntax carries over. Oracle supports shortcuts like
\d,\wand\s, but not word-boundary markers (\b) or lookaheads. - Escape literal metacharacters. To match a real dot or bracket, use
\.or\(. - Mind performance. A regex condition on a large column usually can't use an ordinary index. If a pattern query is hot, consider a function-based index or a cheaper pre-filter such as
LIKE. - Multibyte text works, but write characters directly. Oracle matches on encoded bytes rather than
\uXXXXescapes.
Wrapping up
Start with a constraint on your messiest column, since that's where the payoff is quickest. Then add REGEXP_SUBSTR and REGEXP_REPLACE to your reporting queries. Once you've written a few patterns, you'll find a lot of application-side string handling can simply be deleted.