Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
SlideShare a Scribd company logo
Using Regular Expressions With
Oracle Database
• The database provides a set of SQL functions
that allow you to search and manipulate
strings using regular expressions.
• Apply on datatype that holds character data .
• A regular expression is specified using two
types of characters:
– Meta characters
• operators that specify algorithms
– Literals
• the actual characters to search for.
SQL Regular Expression Function
Returns the number of times a pattern match is found
in an input sting
REGEXP_COUNT
Similar to the LIKE operator, but performs regular
expression matching instead of simple pattern
matching (condition)
REGEXP_LIKE
REGEXP_SUBSTR
REGEXP_INSTR
REGEXP_REPLACE Searches for a regular expression pattern and
replaces it with a replacement string
Searches for a regular expression pattern within a
given string and extracts the matched substring
Searches a string for a regular expression pattern and
returns the position where the match is found
DescriptionFunction or Condition
Name
What are Metacharacters?
• Metacharacters are special characters that
have a special meaning such as
– a wildcard,
– a repeating character,
– a nonmatching character,
– or a range of characters.
• You can use several predefined metacharacter
symbols in the pattern matching.
Using Metacharacters
Metacharcater Operator Name Description
. Any Character -- Dot Matches any character
+ One or More -- Plus
Quantifier
Matches one or more occurrences of the
preceding subexpression
? Zero or One -- Question Mark
Quantifier
Matches zero or one occurrence of the preceding
subexpression
* Zero or More -- Star
Quantifier
Matches zero or more occurrences of the
preceding subexpression
{m} Interval--Exact Count Matches exactlym occurrences of the preceding
subexpression
{m,} Interval--At Least Count Matches at least m occurrences of the preceding
subexpression
{m,n} Interval--Between Count Matches at least m, but not more
than n occurrences of the preceding
subexpression
[ ... ] Matching Character List Matches any character in list ...
[^ ... ] Non-Matching Character List Matches any character not in list ...
Using Metacharacters
Metacharcater Operator Name Description
| Or 'a|b' matches character 'a' or 'b'.
( ... ) Subexpression or Grouping Treat expression ... as a unit. The 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 Anchor Match the subsequent expression only when it
occurs at the beginning of a line.
$ End of Line Anchor Match the preceding expression only when it
occurs at the end of a line.
Regular Expression Function Syntax
REGEXP_LIKE (source_char, pattern [,match_option]
REGEXP_INSTR (source_char, pattern [, position
[, occurrence [, return_option
[, match_option [, subexpr]]]]])
REGEXP_SUBSTR (source_char, pattern [, position
[, occurrence [, match_option
[, subexpr]]]])
REGEXP_REPLACE(source_char, pattern [,replacestr
[, position [, occurrence
[, match_option]]]])
REGEXP_COUNT (source_char, pattern [, position
[, occurrence [, match_option]]])
Regular Expression Function Syntax
• match_options is a text literal that lets you change
the default matching behavior of the function.
– 'i' specifies case-insensitive matching.
– 'c' specifies case-sensitive matching.
– 'n' allows the period (.), to match the newline character.
– 'm' treats the source string as multiple lines
Examples
• Display details of employees whose first name is Steven or Stephen
(where first_name begins with Ste and ends with en and in between is
either v or ph)
SELECT first_name, last_name
FROM employees
WHERE REGEXP_LIKE (first_name, '^Ste(v|ph)en$');
• Last name for those employees with a double vowel in their last name
SELECT last_name
FROM employees
WHERE REGEXP_LIKE (last_name, '([aeiou])1', 'i');
Examples
• examines phone_number, looking for the pattern xxx.xxx.xxxx.
Oracle reformats this pattern with (xxx) xxx-xxxx.
SELECT REGEXP_REPLACE(phone_number,
'([[:digit:]]{3}).([[:digit:]]{3}).([[:digit:]]{4})‘,
'(1) 2-3') "REGEXP_REPLACE“ FROM employees;
• looking for http:// followed by a substring of one or more
alphanumeric characters and optionally, a period (.).
SELECT REGEXP_SUBSTR('http://www.oracle.com/products',
'http://([[:alnum:]]+.?){3,4}/?') "REGEXP_SUBSTR"
FROM DUAL;

More Related Content

11. using regular expressions with oracle database

  • 1. Using Regular Expressions With Oracle Database
  • 2. • The database provides a set of SQL functions that allow you to search and manipulate strings using regular expressions. • Apply on datatype that holds character data .
  • 3. • A regular expression is specified using two types of characters: – Meta characters • operators that specify algorithms – Literals • the actual characters to search for.
  • 4. SQL Regular Expression Function Returns the number of times a pattern match is found in an input sting REGEXP_COUNT Similar to the LIKE operator, but performs regular expression matching instead of simple pattern matching (condition) REGEXP_LIKE REGEXP_SUBSTR REGEXP_INSTR REGEXP_REPLACE Searches for a regular expression pattern and replaces it with a replacement string Searches for a regular expression pattern within a given string and extracts the matched substring Searches a string for a regular expression pattern and returns the position where the match is found DescriptionFunction or Condition Name
  • 5. What are Metacharacters? • Metacharacters are special characters that have a special meaning such as – a wildcard, – a repeating character, – a nonmatching character, – or a range of characters. • You can use several predefined metacharacter symbols in the pattern matching.
  • 6. Using Metacharacters Metacharcater Operator Name Description . Any Character -- Dot Matches any character + One or More -- Plus Quantifier Matches one or more occurrences of the preceding subexpression ? Zero or One -- Question Mark Quantifier Matches zero or one occurrence of the preceding subexpression * Zero or More -- Star Quantifier Matches zero or more occurrences of the preceding subexpression {m} Interval--Exact Count Matches exactlym occurrences of the preceding subexpression {m,} Interval--At Least Count Matches at least m occurrences of the preceding subexpression {m,n} Interval--Between Count Matches at least m, but not more than n occurrences of the preceding subexpression [ ... ] Matching Character List Matches any character in list ... [^ ... ] Non-Matching Character List Matches any character not in list ...
  • 7. Using Metacharacters Metacharcater Operator Name Description | Or 'a|b' matches character 'a' or 'b'. ( ... ) Subexpression or Grouping Treat expression ... as a unit. The 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 Anchor Match the subsequent expression only when it occurs at the beginning of a line. $ End of Line Anchor Match the preceding expression only when it occurs at the end of a line.
  • 8. Regular Expression Function Syntax REGEXP_LIKE (source_char, pattern [,match_option] REGEXP_INSTR (source_char, pattern [, position [, occurrence [, return_option [, match_option [, subexpr]]]]]) REGEXP_SUBSTR (source_char, pattern [, position [, occurrence [, match_option [, subexpr]]]]) REGEXP_REPLACE(source_char, pattern [,replacestr [, position [, occurrence [, match_option]]]]) REGEXP_COUNT (source_char, pattern [, position [, occurrence [, match_option]]])
  • 9. Regular Expression Function Syntax • match_options is a text literal that lets you change the default matching behavior of the function. – 'i' specifies case-insensitive matching. – 'c' specifies case-sensitive matching. – 'n' allows the period (.), to match the newline character. – 'm' treats the source string as multiple lines
  • 10. Examples • Display details of employees whose first name is Steven or Stephen (where first_name begins with Ste and ends with en and in between is either v or ph) SELECT first_name, last_name FROM employees WHERE REGEXP_LIKE (first_name, '^Ste(v|ph)en$'); • Last name for those employees with a double vowel in their last name SELECT last_name FROM employees WHERE REGEXP_LIKE (last_name, '([aeiou])1', 'i');
  • 11. Examples • examines phone_number, looking for the pattern xxx.xxx.xxxx. Oracle reformats this pattern with (xxx) xxx-xxxx. SELECT REGEXP_REPLACE(phone_number, '([[:digit:]]{3}).([[:digit:]]{3}).([[:digit:]]{4})‘, '(1) 2-3') "REGEXP_REPLACE“ FROM employees; • looking for http:// followed by a substring of one or more alphanumeric characters and optionally, a period (.). SELECT REGEXP_SUBSTR('http://www.oracle.com/products', 'http://([[:alnum:]]+.?){3,4}/?') "REGEXP_SUBSTR" FROM DUAL;