Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
40
Добавлен:
16.04.2013
Размер:
4.96 Mб
Скачать

12

Using Regular Expressions With Oracle

Database

This chapter introduces regular expression support for Oracle Database. This chapter covers the following topics:

What are Regular Expressions?

Oracle Database Regular Expression Support

Oracle Database SQL Functions for Regular Expressions

Metacharacters Supported in Regular Expressions

Constructing Regular Expressions

See Also:

Oracle Database SQL Reference for additional details on Oracle Database SQL functions for regular expressions

Oracle Database Globalization Support Guide for details on using SQL regular expression functions in a multilingual environment

Mastering Regular Expressions published by O'Reilly & Associates, Inc.

Using Regular Expressions With Oracle Database 12-1

What are Regular Expressions?

What are Regular Expressions?

Regular expressions specify patterns to search for in string data using standardized syntax conventions. A regular expression can specify complex patterns of character sequences. For example, the following regular expression:

a(b|c)d

searches for the pattern: 'a', followed by either 'b' or 'c', then followed by 'd'. This regular expression matches both 'abd' and 'acd'.

A regular expression is specified using two types of characters:

Metacharacters—operators that specify algorithms for performing the search.

Literals—the actual characters to search for.

Examples of regular expression syntax are given later in this chapter.

Oracle Database Regular Expression Support

Oracle Database implements regular expression support compliant with the POSIX Extended Regular Expression (ERE) specification.

Regular expression support is implemented with a set of Oracle Database SQL functions that allow you to search and manipulate string data. You can use these functions in any environment where Oracle Database SQL is used. See "Oracle Database SQL Functions for Regular Expressions" later in this chapter for more information.

Oracle Database supports a set of common metacharacters used in regular expressions. The behavior of supported metacharacters and related features is described in "Metacharacters Supported in Regular Expressions" on page 12-4.

Note: The interpretation of metacharacters differs between tools that support regular expressions in the industry. 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.

Oracle Database SQL Functions for Regular Expressions

The database provides a set of SQL functions that allow you to search and manipulate strings using regular expressions. You can use these functions on any

12-2 Oracle Database Application Developer's Guide - Fundamentals

Oracle Database SQL Functions for Regular Expressions

datatype that holds character data such as CHAR, NCHAR, CLOB, NCLOB, NVARCHAR2, and VARCHAR2.

A regular expression must be enclosed or wrapped between single quotes. Doing so, ensures that the entire expression is interpreted by the SQL function and can improve the readability of your code.

Table 12–1 gives a brief description of each regular expression function.

Note: As with all text literals used in SQL functions, regular expressions must be enclosed or wrapped between single quotes. If your regular expression includes the single quote character, enter two single quotation marks to represent one single quotation mark within your expression.

Table 12–1 SQL Regular Expression Functions

SQL Function

Description

REGEXP_LIKE

REGEXP_REPLACE

REGEXP_INSTR

REGEXP_SUBSTR

This function searches a character column for a pattern. Use this function in the WHERE clause of a query to return rows matching the regular expression you specify.

See the Oracle Database SQL Reference for syntax details on the REGEXP_LIKE function.

This function searches for a pattern in a character column and replaces each occurrence of that pattern with the pattern you specify.

See the Oracle Database SQL Reference for syntax details on the REGEXP_REPLACE function.

This function searches a string for a given occurrence of a regular expression pattern. You specify which occurrence you want to find and the start position to search from. This function returns an integer indicating the position in the string where the match is found.

See the Oracle Database SQL Reference for syntax details on the REGEXP_INSTR function.

This function returns the actual substring matching the regular expression pattern you specify.

See the Oracle Database SQL Reference for syntax details on the REGEXP_SUBSTR function.

Using Regular Expressions With Oracle Database 12-3

Metacharacters Supported in Regular Expressions

Metacharacters Supported in Regular Expressions

Table 12–2 lists the metacharacters supported for use in regular expressions passed to SQL regular expression functions. Details on the matching behavior of these metacharacters is given in "Constructing Regular Expressions" on page 12-5.

Table 12–2 Metacharacters Supported in Regular Expressions

Metacharacter

 

 

Syntax

Operator Name

Description

 

 

 

.

Any Character —

Matches any character

 

Dot

 

+

One or More — Plus

Matches one or more occurrences of the

 

Quantifier

preceding subexpression

?

Zero or One —

Matches zero or one occurrence of the

 

Question Mark

preceding subexpression

 

Quantifier

 

*

Zero or More —

Matches zero or more occurrences of the

 

Star Quantifier

preceding subexpression

{m}

Interval—Exact

Matches exactly m occurrences of the

 

Count

preceding subexpression

{m,}

Interval—At Least

Matches at least m occurrences of the preceding

 

Count

subexpression

{m,n}

Interval—Between

Matches at least m, but not more than n

 

Count

occurrences of the preceding subexpression

[ ... ]

Matching Character

Matches any character in list ...

 

List

 

[^ ... ]

Non-Matching

Matches any character not in list ...

 

Character List

 

|

Or

'a|b' matches character 'a' or 'b'.

( ... )

Subexpression or

Treat expression ... as a unit. The

 

Grouping

subexpression can be a string of literals or a

 

 

complex expression containing operators.

\n

Backreference

Matches the nth preceding subexpression,

 

 

where n is an integer from 1 to 9.

\

Escape Character

Treat the subsequent metacharacter in the

 

 

expression as a literal.

^

Beginning of Line

Match the subsequent expression only when it

 

Anchor

occurs at the beginning of a line.

12-4 Oracle Database Application Developer's Guide - Fundamentals

Соседние файлы в папке Oracle 10g