regular expression
REGEXP_LIKE performs regular expression matching
SELECT FIRST_NAME
FROM EMPLOYEES
WHERE REGEXP_LIKE(FIRST_NAME,'^al(an|yss)a$','i');--al is followed by an or yss
SELECT FIRST_NAME
FROM EMPLOYEES
WHERE REGEXP_LIKE(FIRST_NAME,'^al(.){2}a$','i');--Any letter in the middle appears twice
SELECT FIRST_NAME
FROM EMPLOYEES
WHERE REGEXP_LIKE(FIRST_NAME,'^al[^y]+a$','i');--The third letter is not allowed to match y all the time
REGEXP_REPLACE performs regular expression replacement
SELECT PHONE_NUMBER,REGEXP_REPLACE(PHONE_NUMBER,'\.','-')AS PHONE FROM EMPLOYEES;--Escape characters must be added \ If not, '.' represents any character
REGEXP_INSTR where the search appears
SELECT STREET_ADDRESS,
REGEXP_INSTR(STREET_ADDRESS,'[[:alpha:]]')AS FIRST_ALPHA_POSITION,-- the first occurrence of any letter
REGEXP_INSTR(STREET_ADDRESS,'[[:digit:]]')AS FIRST_ALPHA_POSITION,-- the position of the first occurrence of any digit
REGEXP_INSTR(STREET_ADDRESS,'[[:alnum:]]')AS FIRST_ALPHA_POSITION-- the first occurrence of any number or letter
FROM LOCATIONS;
Use REGEXP_SUBSTR to control character output
SELECT STREET_ADDRESS,
REGEXP_SUBSTR(STREET_ADDRESS, ' [^ ]+ ')AS ROAD1,
REGEXP_SUBSTR(STREET_ADDRESS, '[^ ]+ ')AS ROAD2,
REGEXP_SUBSTR(STREET_ADDRESS, '[^ ]+')AS ROAD3,
REGEXP_SUBSTR(STREET_ADDRESS, ' [^ ]+(.)+')AS ROAD4,
REGEXP_SUBSTR(STREET_ADDRESS, '[[:alpha:]]')AS ROAD5,
REGEXP_SUBSTR(STREET_ADDRESS, '[[:alpha:]]+')AS ROAD6,
REGEXP_SUBSTR(STREET_ADDRESS, '[[:alpha:]]+(.)')AS ROAD7,
REGEXP_SUBSTR(STREET_ADDRESS, '[[:alpha:]]+(.)+')AS ROAD8
FROM LOCATIONS;
. matches any character in the supported character set, except null
+ matches the preceding subexpression one or more times
| specify a choice of two
'i' case-insensitive match
[[:alpha:]]Any letter
[[:digit:]] any digit
[[:alnum:]]Any letter and number
[[:space:]] any space
subexpression
SELECT
REGEXP_INSTR
('0123456789', --source character
'(123)(4(56)(78))',--expression for matching
1, -- the position to start the search from
1, -- the first match
0, --0 returns the first occurrence position; 1 next starting position
'i', -- case insensitive
1) RESULT -- the first few expressions
FROM DUAL;
A subexpression with parentheses is an expression
For example, (123(4(56)(78))) in (123(4(56)(78))) is the first subexpression
(4(56)(78)) for the second expression (56) for the third (78) for the fourth
Check Constraints and Regular Expressions: Examples
ALTER TABLE EMP8
ADD CONSTRAINT EMAIL_ADDR
CHECK(REGEXP_LIKE(email,'@')) NOVALIDATE;
INSERT INTO EMP8 VALUES
(500,'Christian','Patal','ChrisP2creme.com',
1234567890,'12-Jan-2004','HR-REP',2000,null,102,40);
select * from t50 where REGEXP_LIKE(email,'[^@][@][[:alnum:]]+([\.][[:alnum:]]+|[\.][[:alnum:]]+[\.][[:alnum:]]+)[^\.]$');
ALTER TABLE t50 ADD CONSTRAINT email_addr CHECK(REGEXP_LIKE(email,'[^@][@][[:alnum:]]+([\.][[:alnum:]]+|[\.][[:alnum:]]+[\.][[:alnum:]]+)[^\.]$')) NOVALIDATE;
Table 1: Positioning metacharacters
Metacharacter
description
^
positions the expression to the beginning of a line
$
positions the expression to the end of a line
Table 2: Quantifier or repetition operator
Quantifier
description
*
match 0 or more times
?
match 0 or 1 time
+
match 1 or more times
{m}
matches exactly m times
{m,}
matches at least m times
{m, n}
matches at least m times but not more than n times
Table 3: Predefined POSIX Character Classes
Character class
description
[:alpha:]
alpha character
[:lower:]
Lowercase alphabetic characters
[:upper:]
uppercase alphabetic characters
[:digit:]
Digit
[:alnum:]
Alphanumeric characters
[:space:]
Whitespace characters (suppress printing), such as carriage return, line feed, vertical tab, and form feed
[:punct:]
punctuation character
[:cntrl:]
control character (suppress printing)
[:print:]
printable characters
Table 4: Replacement matching and grouping of expressions
Metacharacter
description
|
replace
delimited replacement option, usually used with grouping operator ()
( )
grouping groups
subexpressions into a substitution unit, quantifier unit, or backreference unit (see the " Backreferences " section)
[char] A
list of characters
represents a list of characters; most metacharacters in a list of characters (with the exception of character classes, ^, and - metacharacters) are understood as literals
Syntax
Description
REGEXP_LIKE(source_string, pattern
[, match_parameter])
source_string supports character data types (CHAR, VARCHAR2, CLOB, NCHAR, NVARCHAR2, and NCLOB, but not LONG). The pattern parameter is another name for a regular expression. match_parameter allows optional parameters (such as handling newlines, preserving multi-line formatting, and providing control over case sensitivity).
Table 6: REGEXP_INSTR function
Syntax
Description
REGEXP_INSTR(source_string, pattern
[, start_position
[, occurrence
[, return_option
[, match_parameter]]]])
This function looks for pattern and returns the first position of the pattern. You can optionally specify the start_position where you want to start the search. The occurrence parameter defaults to 1 unless you specify that you want to find a pattern that occurs next. The default value of return_option is 0, which returns the starting position of the pattern; a value of 1 returns the starting position of the next character that matches the condition.
Table 7: Description of the 5-digit plus 4-digit zip code expression
Syntax
Description
whitespace that must be matched
[:digit:]
POSIX digit class
]
end of character list
The list of {5}
characters is repeated exactly 5 times
(
beginning of subexpression
-
a literal hyphen as it is not a range metacharacter within a character list
[
beginning of character list
[:digit:]
POSIX [:digit:]类
[
beginning of character list
]
end of character list
The list of {4}
characters is repeated exactly 4 times
)
ends the parenthesis, ends the subexpression
?
? The quantifier matches the grouped subexpression 0 or 1 times, making the 4-digit code optional
$
positioning metacharacter, indicating end of line
Table 8: REGEXP_SUBSTR function
Syntax
Description
REGEXP_SUBSTR(source_string, pattern
[, position [, occurrence
[, match_parameter]]])
The REGEXP_SUBSTR function returns the substring that matches the pattern.
TABLE 9: REGEXP_REPLACE FUNCTIONS
Syntax
Description
REGEXP_REPLACE(source_string, pattern
[, replace_string [, position
[,occurrence, [match_parameter]]]])
This function replaces the matching pattern with a specified replace_string, allowing complex "search and replace" operations.
Table 10: Backreference metacharacters
Metacharacter
description
The \digit
backslash
immediately follows a digit between 1 and 9, and the backslash matches the preceding digit-th subexpression enclosed in parentheses.
(Note: The backslash has another meaning in regular expressions, and depending on the context, it may also represent the Escape character.
Table 11: Description of pattern exchange regular expressions
Regular Expression Item
Description
(
beginning of the first subexpression
.
matches any single character except a newline
*
repeat operator, matching the preceding . metacharacter 0 to n times
)
at the end of the first subexpression; the result of the match
is captured in \1 (in this example, the result is Ellen.)
blanks that must exist
(
beginning of the second subexpression
.
matches any single character except a newline
*
repeat operator, matching the preceding . metacharacter 0 to n times
)
at the end of the second subexpression; the result of the match
is captured in \2 (in this example, the result is Hildi.)
blank
(
beginning of the third subexpression
.
matches any single character except a newline
*
repeat operator, matching the preceding . metacharacter 0 to n times
)
at the end of the third subexpression; the result of the match
is captured in \3 (in this example, the result is Smith.)
Table 12: Description of regular expressions for social security numbers
Regular Expression Item
Description
^
First line character (regular expressions cannot have any leading characters before matching.)
(
begins a subexpression and lists alternatives separated by the | metacharacter
[
beginning of character list
[:digit:]
POSIX digit class
]
end of character list
The list of {3}
characters is repeated exactly 3 times
-
hyphen
[
beginning of character list
[:digit:]
POSIX digit class
]
end of character list
The list of {2}
characters is repeated exactly 2 times
-
another hyphen
[
beginning of character list
[:digit:]
POSIX digit class
]
end of character list
The list of {4}
characters is repeated exactly 4 times
|
Replace metacharacters; end the first option and start the next replacement expression
[
beginning of character list
[:digit:]
POSIX digit class
]
end of character list
The list of {9}
characters is repeated exactly 9 times
)
closing parenthesis, closing the subexpression group used for replacement
$
locates metacharacter, indicating end of line; no extra characters can match the pattern