Sub topic 11
When to use REGEXP_SUBSTR, REGEXP_INSTR, REGEXP_REPLACE, or TRANSLATE
Assume that you know basic SELECT statements, WHERE clauses, VARCHAR2, string literals, the DUAL table, and the meaning of NULL. This chapter focuses on choosing a character function when SUBSTR, INSTR, and REPLACE are not enough.
Use ordinary character functions when the rule is based on a fixed position or exact text. Use regular expressions when the rule describes a pattern. Use TRANSLATE when you need to map individual characters.
| Function | Best use | Main question it answers |
|---|---|---|
| `REGEXP_SUBSTR` | Extracting a pattern | What text matches this pattern? |
| `REGEXP_INSTR` | Locating a pattern | Where does this pattern begin? |
| `REGEXP_REPLACE` | Replacing a pattern | How can I replace every matching pattern? |
| `TRANSLATE` | Character-by-character mapping | Which individual characters should change or disappear? |
Choose the simplest function that expresses the rule
INSTR and SUBSTR are usually clearer and faster for a fixed delimiter. For example, splitting [email protected] at @ does not require a regular expression. A regular expression becomes useful when the delimiter or surrounding text can vary.
SELECT REGEXP_SUBSTR(
'Order ID: AB-2048',
'[[:alnum:]]+-[[:digit:]]+'
) AS order_id
FROM dual;The pattern [[:alnum:]]+-[[:digit:]]+ means one or more letters or digits, followed by a hyphen, followed by one or more digits. The result is AB-2048.
Extract patterns with REGEXP_SUBSTR
REGEXP_SUBSTR returns the substring that matches a pattern. Its common arguments are the source text, pattern, starting position, occurrence number, match parameters, and an optional subexpression number.
SELECT REGEXP_SUBSTR(
'Contact: +1 (555) 123-4567',
'[[:digit:]]{3}',
1,
1
) AS first_three_digits
FROM dual;This returns 555, the first three-digit match. The fourth argument selects the occurrence. Change it to 2 to find the second three-digit match.
Use capture groups when you need one part of a larger match. A capture group is a parenthesized part of a regular expression.
SELECT REGEXP_SUBSTR(
'[email protected]',
'([^@]+)@([^@]+)',
1,
1,
NULL,
1
) AS username
FROM dual;The final 1 asks for the first captured group, so the result is user. The pattern itself matches the complete email address.
Locate patterns with REGEXP_INSTR
REGEXP_INSTR returns the position where a pattern begins. It is useful when you need a position for another expression or when you need to test whether a structured pattern exists.
SELECT REGEXP_INSTR(
'Ticket ABC-2048 is open',
'[[:alpha:]]{3}-[[:digit:]]+'
) AS ticket_position
FROM dual;The result is 8, because ABC-2048 starts at character 8. A missing match returns 0, like INSTR.
Use REGEXP_INSTR when the pattern matters. Use INSTR when an exact substring matters. The regular-expression version is more expressive, but its pattern is harder to read and can require more processing.
Replace patterns with REGEXP_REPLACE
REGEXP_REPLACE replaces every matching pattern unless you provide a replacement occurrence. It is useful for variable whitespace, repeated separators, or masked values.
SELECT REGEXP_REPLACE(
'555 123 4567',
'[[:space:]]+',
' '
) AS normalized_phone
FROM dual;The result is 555 123 4567. The pattern matches one or more whitespace characters, and the replacement reduces each run to one space.
You can also use backreferences. A backreference reuses text captured by parentheses.
SELECT REGEXP_REPLACE(
'Smith, John',
'([^,]+),[[:space:]]*(.*)',
'\2 \1'
) AS display_name
FROM dual;The result is John Smith. \1 refers to the first captured group, and \2 refers to the second.
Use TRANSLATE for character maps
TRANSLATE maps each character in a source list to the character at the same position in a replacement list. Extra characters in the source list are removed.
SELECT TRANSLATE('A1-B2-C3', 'ABC-', 'abc') AS converted
FROM dual;This returns a1b2c3. A, B, and C are mapped to lowercase letters. The hyphen has no corresponding replacement character, so it is removed.
Use TRANSLATE for a known set of single characters. Use REGEXP_REPLACE when the rule depends on repetition, character classes, position, or surrounding text.
A practical decision path
Test patterns with representative data, including NULL, missing matches, empty fields, and unexpected punctuation. A regular expression that works for one sample may fail when spacing, case, or delimiters change. Match parameters such as 'i' can enable case-insensitive matching, but adding them should be a deliberate choice.
Exercise
Which function is the best first choice for removing hyphens and spaces from a value when those are the only characters to remove?
Show answer
Use nested REPLACE calls for a few known characters, or TRANSLATE when mapping or removing a known character set is clearer. Use REGEXP_REPLACE only when the rule involves a pattern, such as any run of whitespace.