Skip to content

Regular Expression Functions

Machbase provides PCRE (Perl Compatible Regular Expressions) functions. All regular expression functions operate only on VARCHAR columns.

Quick Reference

FunctionSyntaxDescription
REGEXP_LIKEREGEXP_LIKE(src, pat [, flag])Test for a pattern match
REGEXP_INSTRREGEXP_INSTR(src, pat [, pos [, occ [, ret [, flag]]]])Return the match position
REGEXP_SUBSTRREGEXP_SUBSTR(src, pat [, pos [, occ [, flag]]])Extract a matching substring
REGEXP_REPLACEREGEXP_REPLACE(src, pat [, repl [, pos [, occ [, flag]]]])Replace matching text

match_param (Common Parameter)

ValueDescription
'c'Case-sensitive (default)
'i'Case-insensitive

REGEXP_LIKE

Tests whether a string matches a regular expression. Commonly used in WHERE, it returns a Boolean (1/0).

REGEXP_LIKE(source, pattern)
REGEXP_LIKE(source, pattern, match_param)
  • source: VARCHAR column or expression to test
  • pattern: Constant VARCHAR regular expression
  • match_param: 'c' (case-sensitive, default) or 'i' (case-insensitive)
-- Query messages containing 'error' or 'warn', ignoring case
SELECT *
  FROM sensor_text
 WHERE REGEXP_LIKE(message, 'error|warn', 'i');

-- Query codes starting with digits
SELECT *
  FROM event_log
 WHERE REGEXP_LIKE(code, '^[0-9]+');

-- Validate email format
SELECT name
  FROM users
 WHERE REGEXP_LIKE(email, '^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$');

REGEXP_INSTR

Returns the position of a match, or 0 if none exists. Positions are 1-based.

REGEXP_INSTR(source, pattern)
REGEXP_INSTR(source, pattern, position)
REGEXP_INSTR(source, pattern, position, occurrence)
REGEXP_INSTR(source, pattern, position, occurrence, return_pos)
REGEXP_INSTR(source, pattern, position, occurrence, return_pos, match_param)
ParameterDescription
sourceVARCHAR to search
patternConstant VARCHAR regular expression
positionStarting position (at least 1; default: 1)
occurrenceMatch occurrence to find (at least 1; default: 1)
return_pos0: Start position; 1: Position after the match
match_param'c' or 'i'
-- Position after the first 'The' match, ignoring case
SELECT REGEXP_INSTR('TechOnTheNet', 'The', 1, 1, 1, 'i');
-- Result: 10 (position after 'The')

REGEXP_SUBSTR

Returns the substring matching the regular expression, or NULL if none exists.

REGEXP_SUBSTR(source, pattern)
REGEXP_SUBSTR(source, pattern, position)
REGEXP_SUBSTR(source, pattern, position, occurrence)
REGEXP_SUBSTR(source, pattern, position, occurrence, match_param)
ParameterDescription
sourceVARCHAR to search
patternConstant VARCHAR regular expression
positionStarting position (at least 1; default: 1)
occurrenceMatch occurrence to find (at least 1; default: 1)
match_param'c' or 'i'
-- Extract the second vowel, ignoring case
SELECT REGEXP_SUBSTR('TechOnTheNet', 'a|e|i|o|u', 1, 2, 'i');
-- Result: 'O'

-- Extract the first octet from an IP address
SELECT REGEXP_SUBSTR(ip_str, '[0-9]+', 1, 1) FROM log_table;

-- Extract an error code from a log
SELECT REGEXP_SUBSTR(message, 'ERR-[0-9]+') FROM event_log;

REGEXP_REPLACE

Replaces matching text with the specified string.

REGEXP_REPLACE(source, pattern)
REGEXP_REPLACE(source, pattern, replacement)
REGEXP_REPLACE(source, pattern, replacement, position)
REGEXP_REPLACE(source, pattern, replacement, position, occurrence)
REGEXP_REPLACE(source, pattern, replacement, position, occurrence, match_param)
ParameterDescription
sourceTarget VARCHAR
patternConstant VARCHAR regular expression
replacementReplacement string; omitted means remove the match
positionStarting position (at least 1; default: 1)
occurrence0: Replace all; positive n: replace the nth match only (default: 0)
match_param'c' or 'i'
-- Replace the second vowel with 'Z', ignoring case
SELECT REGEXP_REPLACE('TechOnTheNet', 'a|e|i|o|u', 'Z', 1, 2, 'i');
-- Result: 'TechZnTheNet'

-- Remove all digits
SELECT REGEXP_REPLACE(code, '[0-9]', '') FROM log_table;

-- Normalize consecutive whitespace to one space
SELECT REGEXP_REPLACE(message, '\s+', ' ') FROM event_log;

PCRE Basics

PatternDescriptionExample
.Any single charactera.c → abc, aXc
*Zero or more repetitionsab*c → ac, abc, abbc
+One or more repetitionsab+c → abc, abbc
?Zero or one occurrencecolou?r → color, colour
^Start of string^error
$End of string\.log$
[abc]Character class[aeiou]
[^abc]Negated character class[^0-9]
\dDigit ([0-9])\d+
\wWord character\w+
\sWhitespace\s+
a|ba or berror|warn
(abc)Group(foo)+
{n,m}n to m repetitions\d{3,5}

Comparison with SEARCH / ESEARCH

FeatureREGEXP_LIKESEARCH / ESEARCH
Applicable TypeVARCHARTEXT (full-text index)
Regex supportO (PCRE)X (keyword search)
Index useXO
Large text volumesLimitedRecommended

For keyword searches over large text volumes, TEXT and SEARCH provide better performance. Use VARCHAR and REGEXP_LIKE when regex pattern matching is required.

Last updated on