Skip Headers
Oracle® Application Server PL/SQL Web Toolkit Reference
10g Release 2 (10.1.2)
B15896-01
  Go To Documentation Library
Home
Go To Product List
Solution Area
Go To Table Of Contents
Contents
Go To Index
Index

Previous
Previous
Next
Next
 

6 The owa_pattern Package

The owa_pattern package in the PL/SQL Web Toolkit locates text patterns within strings and replaces the matched string with another string. Use regular expressions with the subprograms in this package.

6.1 Subprograms

Regular Expressions - this section describes the special characters, quantifiers, and flags used in forming regular expressions.

owa_pattern.amatch function - determines if a string contains the specified pattern. It lets you specify where in the string the match has to occur.

owa_pattern.change function and procedure - replaces a pattern within a string. If you call it as a function it returns the number of times the regular expression was found and replaced.

owa_pattern.getpat procedure - generates a pattern data type from a VARCHAR2 type.

owa_pattern.match function - determines if a string contains the specified pattern.

owa_pattern.pattern data type - data type to store regular expressions.

These subprograms are overloaded. That is, there are several versions of each, distinguished by the parameters they use. Specifically, there are six versions of MATCH, and four each of AMATCH and CHANGE. The subprograms use the following parameters:

6.2 Regular Expressions

Specify a regular expression by creating the string you want to match interspersed with various wildcard tokens and quantifiers.

6.2.1 Wildcard Tokens

Wildcard tokens match something other than themselves:

Table 6-1 Wildcard tokens recognized by owa_pattern package

Token Description
^

Matches newline or the beginning of the target

$

Matches newline or the end of the target

\n

Matches newline

.

Matches any character except newline

\t

Matches tab

\d 

Matches digits [0-9]

\D

Matches non-digits [not 0-9]

\w

Matches word characters (0-9, a-z, A-Z, or _)

\W

Matches non-word characters (not 0-9, a-z, A-Z, or _)

\s

Matches whitespace characters (blank, tab, or newline).

\S

Matches non-whitespace characters (not blank, tab, or newline)

\b

Matches "word" boundaries (between \w and \W)

\x<HEX>

Matches the value in the current character set of the two hexadecimal digits

\<OCT>

Matches the value in the current character set of the two or three octal digits

\

Followed by any character not covered by another case matches that character

&

Applies only to CHANGE. This causes the string that matched the regular expression to be included in the string that replaces it. This differs from the other tokens in that it specifies how a target is changed rather than how it is matched. This is explained further under CHANGE.


6.2.2 Quantifiers

Any tokens except & can have their meaning extended by any of the following quantifiers. You can also apply these quantifiers to literals:

Table 6-2 Quantifiers

Quantifier Description
?

0 or 1 occurrence(s)

*

0 or more occurrences

+

1 or more occurrence(s)

{n}

Exactly n occurrences

(n,}

At least n occurrences

{n,m}

At least n, but not more than m, occurrences


6.2.3 Flags

In addition to targets and regular expressions, the owa_pattern functions and procedures use flags to affect how they are interpreted.

Table 6-3 Flags

Flag Description
i

This indicates a case-insensitive search.

g

This applies only to CHANGE. It indicates a global replace. That is, all portions of the target that match the regular expression are replaced.


6.3 owa_pattern.amatch function

This function specifies if a pattern occurs in a particular location in a string. There are four versions to this function:

Table 6-4 owa_pattern.amatch function

Properties Definitions

Syntax:

owa_pattern.amatch(
   line           in       varchar2
   from_loc       in       integer
   pat            in       varchar2
   flags          in       varchar2   DEFAULT NULL) return integer;


owa_pattern.amatch(
   line           in       varchar2
   from_loc       in       integer
   pat            in out   pattern
   flags          in       varchar2   DEFAULT NULL) return integer;


owa_pattern.amatch(
   line           in       varchar2
   from_loc       in       integer
   pat            in   varchar2
   backrefs       out      owa_text.vc_arr
   flags          in       varchar2   DEFAULT NULL) return integer;


owa_pattern.amatch(
   line           in       varchar2
   from_loc       in       integer
   pat            in out   pattern
   backrefs       out      owa_text.vc_arr
   flags          in       varchar2   DEFAULT NULL) return integer;

Parameters:

line - the text to search in.

from_loc - the location (in number of characters) in line where the search is to begin.

pat - the string to match. It can contain regular expressions. This can be either a VARCHAR2 or a pattern. If it is a pattern, the output value of this parameter is the pattern matched.

backrefs - the text that is matched. Each token that is matched is placed in a cell in the owa_text.vc_arr data type PL/SQL table.

flags - whether or not the search is case-sensitive. If the value of this parameter is "i", the search is case-insensitive. Otherwise the search is case-sensitive.

Returns:

The index of the character after the end of the match, counting from the beginning of line. If there was no match, the function returns 0.


6.4 owa_pattern.change function and procedure

This function or procedure searches and replaces a string or multi_line data type. If multiple overlapping strings match the regular expression, this function takes the longest match.

Table 6-5 owa_pattern.change function and procedure

Properties Definitions

Syntax:

/* function */
owa_pattern.change(
   line           in out    varchar2
   from_str       in        varchar2
   to_str         in        varchar2
   flags          in        varchar2   DEFAULT NULL) return integer;


/* procedure */
owa_pattern.change(
   line           in out   varchar2
   from_str       in       varchar2
   to_str         in       varchar2
   flags          in       varchar2   DEFAULT NULL);


/* function */
owa_pattern.change(
   mline          in out   owa_text.multi_line
   from_str       in       varchar2
   to_str         in       varchar2
   flags          in       varchar2   DEFAULT NULL) return integer;


/* procedure */
owa_pattern.change(
   mline          in out   owa_text.multi_line
   from_str       in       varchar2
   to_str         in       varchar2
   flags          in       varchar2   DEFAULT NULL);

Parameters:

line - the text to search in. The output value of this parameter is the altered string.

mline - the text to search in. This is a owa_text.multi_line data type data type. The output value of this parameter is the altered string.

from_str - the regular expression to replace.

to_str - the substitution pattern.

flags - whether or not the search is case-sensitive, and whether or not changes are to be made globally. If "i" is specified, the search is case-insensitive. If "g" is specified, changes are made to all matches. Otherwise, the function stops after the first substitution is made.

Returns:

As a function, it returns the number of substitutions made. If the flag `g' is not used, this number can only be 0 or 1 and only the first match is replaced. The flag `g' specifies to replace all matches with the regular expression.

Example:

Example 1:

owa_pattern.change('Cats in pajamas', 'C.+in', '& red ')

The regular expression matches the substring `Cats in'. It then replaces this string with `& red'. The ampersand character (&) indicates `Cats in', since that's what matched the regular expression. Thus, this procedure replaces the string `Cats in pajamas' with 'Cats in red'. If you called this as a function instead of a procedure, the value it returns is 1, indicating that a single substitution has been made.

Example 2:

create or replace procedure test_pattern as theline VARCHAR2(256);
 num_found integer;
begin
theline := 'what is the goal?';
 num_found := owa_pattern.change(theline, 'goal', 'idea', 'g');
htp.print(num_found); -- num_found is 1
htp.print(theline); -- theline is 'what is the idea?'
end;
/
show errors

6.5 owa_pattern.getpat procedure

This procedure converts a VARCHAR2 string into a owa_pattern.pattern data type.

Table 6-6 owa_pattern.getpat procedure

Properties Definitions

Syntax:

owa_pattern.getpat(arg in VARCHAR2, pat in out pattern);

Parameters:

arg - the string to convert.

pat - the owa_pattern.pattern data type initialized with arg.

Returns:

None.


6.6 owa_pattern.match function

This function determines if a string contains the specified pattern. The pattern can contain regular expressions. If multiple overlapping strings can match the regular expression, this function takes the longest match.

The regular expression in this function can be either a VARCHAR2 or a owa_pattern.pattern data type. Create a owa_pattern.pattern data type from a string using the owa_pattern.getpat procedure.

Create a multi_line data type from a long string using the owa_text.stream2multi procedure. If a multi_line is used, the rlist parameter specifies a list of chunks where matches were found.

If the line is a string and not a multi_line, you can add an optional output parameter called backrefs. This parameter is a row_list that holds each string in the target that was matched by a sequence of tokens in the regular expression.

Table 6-7 owa_pattern.match function

Properties Definitions

Syntax:

owa_pattern.match(
   line           in       varchar2
   pat            in       varchar2
   flags          in       varchar2   DEFAULT NULL) return boolean;


owa_pattern.match(
   line           in       varchar2
   pat            in out   pattern
   flags          in       varchar2   DEFAULT NULL) return boolean;


owa_pattern.match(
   line           in       varchar2
   pat            in       varchar2
   backrefs       out      owa_text.vc_arr
   flags          in       varchar2   DEFAULT NULL) return boolean;


owa_pattern.match(
   line           in       varchar2
   pat            in out   pattern
   backrefs       out      owa_text.vc_arr
   flags          in       varchar2   DEFAULT NULL) return boolean;


owa_pattern.match(
   mline          in       owa_text.multi_line
   pat            in       varchar2
   rlist          out      owa_text.row_list
   flags          in       varchar2   DEFAULT NULL) return boolean;


owa_pattern.match(
   mline          in       owa_text.multi_line
   pat            in out   pattern
   rlist          out      owa_text.row_list
   flags          in       varchar2   DEFAULT NULL) return boolean;

Parameters:

line - the text to search in.

mline - the text to search in. This is a owa_text.multi_line data type data type.

pat - the pattern to match. This is either a VARCHAR2 or a owa_pattern.pattern data type data type. It it is a pattern, the output value of this parameter is the pattern matched.

backrefs - the text that is matched. Each token that is matched is placed in a cell in the owa_text.vc_arr data type PL/SQL table. This parameter is a row_list that holds each string in the target that was matched by a sequence of tokens in the regular expression.

rlist - an output parameter containing a list of matches.

flags - whether or not the search is case-sensitive. If the value of this parameter is "i", the search is case-insensitive. Otherwise the search is case-sensitive.

Returns:

TRUE if a match was found, FALSE otherwise.

Examples:

KAZOO is the target where it is searching for the zoo.* regular expression. The period indicates any character other than newline, and the asterisk matches 0 or more of the preceding characters. In this case, it matches any character other than the newline.

Therefore, this regular expression specifies that a matching target consists of zoo, followed by any set of characters neither ending in nor including a newline (which does not match the period). The i flag indicates to ignore case in the search. In this case, the function returns TRUE, which indicates that a match had been found.

boolean foundMatch;
foundMatch := owa_pattern.match('KAZOO', 'zoo.*', 'i');

The following example searches for the string "goal" followed by any number of characters in sometext. If found,

sometext  VARCHAR2(256);
pat       VARCHAR2(256);

sometext := 'what is the goal?'
pat := 'goal.*';
if owa.pattern.match(sometext, pat) then
   htp.print('Match found');
else
   htp.print('Match not found');
end if;

6.7 owa_pattern.pattern data type

You can use a pattern as both an input and output parameter. Thus, you can pass the same regular expression to OWA_PATTERN function calls, and it only has to be parsed once.

Table 6-8 owa_pattern.pattern data type

Properties Definitions

Syntax:

owa_pattern.pattern - data type

Returns:

Not applicable.