Oracle® Database Application Developer's Guide - Fundamentals 10g Release 2 (10.2) Part Number B14251-01 |
|
|
View PDF |
This chapter introduces regular expression support for Oracle Database. This chapter covers the following topics:
See Also:
Oracle Database SQL Reference for information about Oracle Database SQL functions for regular expressions
Oracle Database Globalization Support Guide for details on using SQL regular expression functions in a multilingual environment
Oracle Regular Expressions Pocket Reference by Jonathan Gennick, O'Reilly & Associates
Mastering Regular Expressions by Jeffrey E. F. Friedl, O'Reilly & Associates
This section contains the following topics:
Regular expressions enable you to search for patterns in string data by using standardized syntax conventions. You specify a regular expression by means of the following types of characters:
Metacharacters, which are operators that specify search algorithms
Literals, which are the characters for which you are searching
A regular expression can specify complex patterns of character sequences. For example, the following regular expression searches for the literals f
or ht
, the t
literal, the p
literal optionally followed by the s
literal, and finally the colon (:
) literal:
(f|ht)tps?:
The parentheses are metacharacters that group a series of pattern elements to a single element; the pipe symbol (|
) matches one of the alternatives in the group. The question mark (?
) is a metacharacter indicating that the preceding pattern, in this case the s
character, is optional. Thus, the preceding regular expression matches the http:
, https:
, ftp:
, and ftps:
strings.
Regular expressions are a powerful text processing component of programming languages such as Perl and Java. For example, a Perl script can process each HTML file in a directory, read its contents into a scalar variable as a single string, and then use regular expressions to search for URLs in the string. One reason that many developers write in Perl is for its robust pattern matching functionality.
Oracle's support of regular expressions enables developers to implement complex match logic in the database. This technique is useful for the following reasons:
By centralizing match logic in Oracle Database, you avoid intensive string processing of SQL results sets by middle-tier applications. For example, life science customers often rely on Perl to do pattern analysis on bioinformatics data stored in huge databases of DNAs and proteins. Previously, finding a match for a protein sequence such as [AG].{4}GK[ST]
would be handled in the middle tier. The SQL regular expression functions move the processing logic closer to the data, thereby providing a more efficient solution.
Prior to Oracle Database 10g, developers often coded data validation logic on the client, requiring the same validation logic to be duplicated for multiple clients. Using server-side regular expressions to enforce constraints solves this problem.
The built-in SQL and PL/SQL regular expression functions and conditions make string manipulations more powerful and less cumbersome than in previous releases of Oracle Database.
Oracle Database implements regular expression support with a set of Oracle Database SQL functions and conditions that enable you to search and manipulate string data. You can use these functions in any environment that supports Oracle Database SQL. You can use these functions on a text literal, bind variable, or any column that holds character data such as CHAR
, NCHAR
, CLOB
, NCLOB
, NVARCHAR2
, and VARCHAR2
(but not LONG
).
Table 4-1 gives a brief description of the regular expression functions and conditions.
Table 4-1 SQL Regular Expression Functions and Conditions
A string literal in a REGEXP
function or condition conforms to the rules of SQL text literals. By default, regular expressions must be enclosed in single quotes. If your regular expression includes the single quote character, then enter two single quotation marks to represent one single quotation mark within the expression. This technique ensures that the entire expression is interpreted by the SQL function and improves the readability of your code. You can also use the q-quote syntax to define your own character to terminate a text literal. For example, you could delimit your regular expression with the pound sign (#
) and then use a single quote within the expression.
Note:
If your expression comes from a column or a bind variable, then the same rules for quoting do not apply.See Also:
Oracle Database SQL Reference for syntax, descriptions, and examples of the REGEXP
functions and conditions
Oracle Database SQL Reference for a discussion of character literals
Oracle's implementation of regular expressions conforms to the following standards:
IEEE Portable Operating System Interface (POSIX) standard draft 1003.2/D11.2
Unicode Regular Expression Guidelines of the Unicode Consortium
Oracle Database follows the exact syntax and matching semantics for these operators as defined in the POSIX standard for matching ASCII (English language) data. You can find the POSIX standard draft at the following URL:
http://www.opengroup.org/onlinepubs/007908799/xbd/re.html
Oracle Database enhances regular expression support in the following ways:
Extends the matching capabilities for multilingual data beyond what is specified in the POSIX standard.
Adds support for the common Perl regular expression extensions that are not included in the POSIX standard but do not conflict with it. Oracle Database provides built-in support for some of the most heavily used Perl regular expression operators, for example, character class shortcuts, the non-greedy modifier, and so on.
Oracle Database supports a set of common metacharacters used in regular expressions. The behavior of supported metacharacters and related features is described in "Regular Expression Metacharacters in Oracle Database".
Note:
The interpretation of metacharacters differs between tools that support regular expressions. If you are porting regular expressions from another environment to Oracle Database, ensure that the regular expression syntax is supported and the behavior is what you expect.This section contains the following topics:
Table 4-2 lists the list of metacharacters supported for use in regular expressions passed to SQL regular expression functions and conditions. These metacharacters conform to the POSIX standard; any differences in behavior from the standard are noted in the "Description" column.
Table 4-2 POSIX Metacharacters in Oracle Database Regular Expressions
Syntax | Operator Name | Description | Example |
---|---|---|---|
|
Any Character — Dot |
Matches any character in the database character set. If the Note: In the POSIX standard, this operator matches any English character except NULL and the newline character. |
The expression |
|
One or More — Plus Quantifier |
Matches one or more occurrences of the preceding subexpression. |
The expression |
|
Zero or One — Question Mark Quantifier |
Matches zero or one occurrence of the preceding subexpression. |
The expression |
|
Zero or More — Star Quantifier |
Matches zero or more occurrences of the preceding subexpression. By default, a quantifier match is greedy because it matches as many times as possible while still allowing the rest of the match to succeed. |
The expression |
|
Interval—Exact Count |
Matches exactly |
The expression |
|
Interval—At Least Count |
Matches at least |
The expression |
|
Interval—Between Count |
Matches at least |
The expression |
|
Matching Character List |
Matches any single character in the list within the brackets. The following operators are allowed within the list, but other metacharacters included are treated as literals:
A dash ( Note: In the POSIX standard, a range includes all collation elements between the start and end of the range in the linguistic definition of the current locale. Thus, ranges are linguistic rather than byte values ranges; the semantics of the range expression are independent of character set. In Oracle Database, the linguistic range is determined by the |
The expression |
|
Non-Matching Character List |
Matches any single character not in the list within the brackets. Characters not in the non-matching character list are returned as a match. Refer to the description of the Matching Character List operator for an account of metacharacters allowed in the character list. |
The expression The expression |
|
Or |
Matches one of the alternatives. |
The expression |
|
Subexpression or Grouping |
Treats the expression within parentheses as a unit. The subexpression can be a string of literals or a complex expression containing operators. |
The expression |
|
Backreference |
Matches the nth preceding subexpression, that is, whatever is grouped within parentheses, where Oracle supports the backreference expression in the regular expression pattern and the replacement string of the |
The expression A backreference enables you to search for a repeated string without knowing the actual string ahead of time. For example, the expression |
|
Escape Character |
Treats the subsequent metacharacter in the expression as a literal. Use a backslash (\) to search for a character that is normally treated as a metacharacter. Use consecutive backslashes ( |
The expression |
|
Beginning of Line Anchor |
Matches the beginning of a string (default). In multiline mode, it matches the beginning of any line within the source string. |
The expression |
|
End of Line Anchor |
Matches the end of a string (default). In multiline mode, it matches the beginning of any line within the source string. |
The expression |
|
POSIX Character Class |
Matches any character belonging to the specified POSIX character Note: In English regular expressions, range expressions often indicate a character class. For example, |
The expression |
|
POSIX Collating Element Operator |
Specifies a collating element to use in the regular expression. The |
The expression |
|
POSIX Character Equivalence Class |
Matches all characters that are members of the same character equivalence class in the current locale as the specified The character equivalence class must occur within a character list, so the character equivalence class is always nested within the brackets for the character list in the regular expression. Usage of character equivalents depends on how canonical rules are defined for your database locale. Refer to the Oracle Database Globalization Support Guide for more information on linguistic sorting and string searching. |
The expression |
See Also:
Oracle Database SQL Reference for syntax, descriptions, and examples of theREGEXP
functions and conditionsWhen applied to multilingual data, Oracle's implementation of the POSIX operators extends beyond the matching capabilities specified in the POSIX standard. Table 4-3 shows the relationship of the operators in the context of the POSIX standard.
The first column lists the supported operators.
The second column indicates whether the POSIX standard for Basic Regular Expression (BRE) defines the operator.
The third column indicates whether the POSIX standard for Extended Regular Expression (ERE) defines the operator.
The fourth column indicates whether the Oracle Database implementation extends the operator's semantics for handling multilingual data.
Oracle Database lets you enter multibyte characters directly, if you have a direct input method, or use functions to compose the multibyte characters. You cannot use the Unicode hexadecimal encoding value of the form \xxxx
. Oracle evaluates the characters based on the byte values used to encode the character, not the graphical representation of the character.
Table 4-3 POSIX and Multilingual Operator Relationships
Operator | POSIX BRE syntax | POSIX ERE Syntax | Multilingual Enhancement |
---|---|---|---|
|
Yes |
Yes |
-- |
|
Yes |
Yes |
-- |
|
-- |
Yes |
-- |
|
-- |
Yes |
-- |
|
-- |
Yes |
-- |
|
Yes |
Yes |
Yes |
|
Yes |
Yes |
Yes |
|
Yes |
Yes |
Yes |
|
Yes |
Yes |
Yes |
|
Yes |
Yes |
-- |
|
Yes |
Yes |
-- |
|
Yes |
Yes |
-- |
|
Yes |
Yes |
-- |
|
Yes |
Yes |
Yes |
|
Yes |
Yes |
Yes |
|
Yes |
Yes |
Yes |
|
Yes |
Yes |
Yes |
Table 4-4 describes Perl-influenced metacharacters supported in Oracle Database regular expression functions and conditions. These metacharacters are not in the POSIX standard, but are common at least partly due to the popularity of Perl. Note that Perl character class matching is based on the locale model of the operating system, whereas Oracle Database regular expressions are based on the language-specific data of the database. In general, a regular expression involving locale data cannot be expected to produce the same results between Perl and Oracle Database.
Table 4-4 Perl-Influenced Extensions in Oracle Regular Expressions
Reg. Exp. | Matches . . . | Example |
---|---|---|
|
A digit character. It is equivalent to the POSIX class |
The expression |
|
A non-digit character. It is equivalent to the POSIX class |
The expression |
|
A word character, which is defined as an alphanumeric or underscore ( |
The expression |
|
A non-word character. It is equivalent to the POSIX class |
The expression |
|
A whitespace character. It is equivalent to the POSIX class |
The expression |
|
A non-whitespace character. It is equivalent to the POSIX class |
The expression |
|
Only at the beginning of a string. In multi-line mode, that is, when embedded newline characters in a string are considered the termination of a line, |
The expression |
|
Only at the end of string or before a newline ending a string. In multi-line mode, that is, when embedded newline characters in a string are considered the termination of a line, |
In the expression |
|
Only at the end of a string. |
In the expression |
|
The preceding pattern element 0 or more times (non-greedy). Note that this quantifier matches the empty string whenever possible. |
The expression |
|
The preceding pattern element 1 or more times (non-greedy). |
The expression |
|
The preceding pattern element 0 or 1 time (non-greedy). Note that this quantifier matches the empty string whenever possible. |
The expression |
|
The preceding pattern element exactly |
The expression |
|
The preceding pattern element at least |
The expression |
|
At least |
The expression |
The Oracle Database regular expression functions and conditions support the pattern matching modifiers described in Table 4-5.
Table 4-5 Pattern Matching Modifiers
Mod. | Description | Example |
---|---|---|
|
Specifies case-insensitive matching. |
The following regular expression returns REGEXP_SUBSTR('AbCd', 'abcd', 1, 1, 'i') |
|
Specifies case-sensitive matching. |
The following regular expression fails to match: REGEXP_SUBSTR('AbCd', 'abcd', 1, 1, 'c') |
n |
Allows the period (.), which by default does not match newlines, to match the newline character. |
The following regular expression matches the string, but would not match if the REGEXP_SUBSTR('a'||CHR(10)||'d', 'a.d', 1, 1, 'n') |
m |
Performs the search in multi-line mode. The metacharacter |
The following regular expression returns REGEXP_SUBSTR('ab'||CHR(10)||'ac', '^a.', 1, 2, 'm') |
|
Ignores whitespace characters in the regular expression. By default, whitespace characters match themselves. |
The following regular expression returns REGEXP_SUBSTR('abcd', 'a b c d', 1, 1, 'x') |
This section contains the following scenarios:
Regular expressions are a useful way to enforce integrity constraints. For example, suppose that you want to ensure that phone numbers are entered into the database in a standard format. Example 4-1 creates a contacts
table and adds a check constraint to the p_number
column to enforce the following format mask:
(XXX) XXX-XXXX
Example 4-1 Enforcing a Phone Number Format with Regular Expressions
CREATE TABLE contacts ( l_name VARCHAR2(30), p_number VARCHAR2(30) CONSTRAINT p_number_format CHECK ( REGEXP_LIKE ( p_number, '^\(\d{3}\) \d{3}-\d{4}$' ) ) );
Table 4-6 explains the elements of the regular expression.
Table 4-6 Explanation of the Regular Expression Elements in Example 4-1
Regular Expression Element | Matches . . . |
---|---|
|
The beginning of the string. |
|
A left parenthesis. The backward slash ( |
|
Exactly three digits. |
|
A right parenthesis. The backward slash ( |
(space character) |
A space character. |
|
Exactly three digits. |
|
A hyphen. |
|
Exactly four digits. |
|
The end of the string. |
Example 4-2 shows a SQL script that attempts to insert seven phone numbers into the contacts
table. Only the first two INSERT
statements use a format that conforms to the p_number_format
constraint; the remaining statements generate check constraint errors.
Example 4-2 insert_contacts.sql
-- first two statements use valid phone number format INSERT INTO contacts (p_number) VALUES( '(650) 555-5555' ); INSERT INTO contacts (p_number) VALUES( '(215) 555-3427' ); -- remaining statements generate check contraint errors INSERT INTO contacts (p_number) VALUES( '650 555-5555' ); INSERT INTO contacts (p_number) VALUES( '650 555 5555' ); INSERT INTO contacts (p_number) VALUES( '650-555-5555' ); INSERT INTO contacts (p_number) VALUES( '(650)555-5555' ); INSERT INTO contacts (p_number) VALUES( ' (650) 555-5555' ); /
As explained in Table 4-2, back references store matched subexpressions in a temporary buffer, thereby enabling you to reposition characters. You access buffers with the \n
notation, where \n
is a number between 1 and 9. Each subexpression is contained in parentheses and is numbered from left to right.
Example 4-3 creates a famous_people
table and populates the famous_people.names
column with names in different formats.
Example 4-3 Using Back References to Reposition Characters
CREATE TABLE famous_people ( names VARCHAR2(30) ); -- populate table with data INSERT INTO famous_people VALUES ('John Quincy Adams'); INSERT INTO famous_people VALUES ('Harry S. Truman'); INSERT INTO famous_people VALUES ('John Adams'); INSERT INTO famous_people VALUES (' John Quincy Adams'); INSERT INTO famous_people VALUES ('John_Quincy_Adams'); COMMIT;
Example 4-4 shows a query that repositions names in the format "first middle last" to the format "last, first middle". It ignores names not in the format "first middle last".
Example 4-4 Using Back References to Reposition Characters
SELECT names "names", REGEXP_REPLACE(names, '^(\S+)\s(\S+)\s(\S+)$', '\3, \1 \2') AS "names after regexp" FROM famous_people;
Table 4-7 explains the elements of the regular expression.
Table 4-7 Explanation of the Regular Expression Elements in Example 4-4
Regular Expression Element | Description |
---|---|
|
Matches the beginning of the string. |
|
Matches the end of the string. |
|
Matches one or more non-space characters. The parentheses are not escaped so they function as a grouping expression. |
|
Matches a whitespace character. |
|
Substitutes the first subexpression, that is, the first group of parentheses in the matching pattern. |
|
Substitutes the second subexpression, that is, the second group of parentheses in the matching pattern. |
|
Substitutes the third subexpression, that is, the third group of parentheses in the matching pattern. |
|
Inserts a comma character. |
Example 4-5 shows the result set of the query in Example 4-4. The regular expression matched only the first two rows.
Example 4-5 Result Set of Regular Expression Query
names ------------------------------ names after regexp ------------------------------ John Quincy Adams Adams, John Quincy Harry S. Truman Truman, Harry S. John Adams John Adams John Quincy Adams John Quincy Adams John_Quincy_Adams John_Quincy_Adams