sipxportlib
Version 3.3
|
#include <UtlRegex.h>
Constructors, Destructor, and Expression Information | |
static const unsigned long int | MAX_RECURSION = SIPX_MAX_REGEX_RECURSION |
Default maximum for the recursion depth in searches. More... | |
RegEx (const char *regex, int options=0, unsigned long int maxDepth=MAX_RECURSION) | |
Compile a regular expression to create the matching object. More... | |
RegEx (const RegEx &) | |
Construct from a constant regex to save compilation time. More... | |
~RegEx () | |
int | SubStrings (void) const |
Count the number of possible substrings returned by this expression. More... | |
Searching | |
The searching methods apply a compiled regular expression to a subject string. All searching methods return a boolean result indicating whether or not some match was found in the subject. To get information about the match, use the Results methods. | |
bool | Search (const char *subject, int len=-1, int options=0) |
Search a string for matches to this regular expression. More... | |
bool | SearchAt (const char *subject, int offset, int len=-1, int options=0) |
Search a string starting at some offset for matches to this regular expression. More... | |
bool | SearchAgain (int options=0) |
Repeat the last search operation, starting immediately after the previous match. More... | |
Results | |
The results methods provide information about the matches based on the results of the most recent Searching method call. It is an error to call any of these methods unless the most recent Searching call returned 'true'. The substring index must be less than the result of RegEx::SubStrings on the regular expression, but may also be zero or -1 as follows:
| |
int | Matches () |
Get the maximum substring value from the most recent search. More... | |
bool | MatchString (UtlString *matched, int i=0) |
Append a match from the last search operation to a UtlString. More... | |
bool | Match (const int i, int &offset, int &length) |
Get the position and length of a match in the subject. More... | |
int | MatchStart (const int i) |
Get the position of a match in the subject. More... | |
bool | BeforeMatchString (UtlString *before) |
Append string preceeding the most recently matched value to a UtlString. More... | |
bool | AfterMatchString (UtlString *before) |
Append string following the most recently matched value to a UtlString. More... | |
int | AfterMatch (int i) |
Get the offset of the first character past the matched value. More... | |
const char * | Match (int i=0) |
Get a string matched by a previous search. More... | |
RegEx implements Perl-compatible regular expressions
A simple and small C++ wrapper for PCRE. PCRE (or libprce) is the Perl Compatible Regular Expression library. http://www.pcre.org/
Adapted for the sipXportLib from the regex.hpp wrapper:
regex.hpp 1.0 Copyright (c) 2003 Peter Petersen (pp@on) Simple C++ wrapper for PCRE -tim e.de
This source file is freeware. You may use it for any purpose without restriction except that the copyright notice as the top of this file as well as this paragraph may not be removed or altered.
Original wrapper by Peter Petersen, adapted to sipX by Scott Lawrence
The regular expression is compiled in the constructor, and then may be applied to target strings using one of the Search interfaces. The results are obtained using the Results interfaces.
This class is a wrapper around the PCRE package (see the project INSTALL for a pointer to where PCRE can be found). All the Options variables are identical to those in pcre.h
RegEx | ( | const char * | regex, |
int | options = 0 , |
||
unsigned long int | maxDepth = MAX_RECURSION |
||
) |
Compile a regular expression to create the matching object.
If compiling the regular expression fails, an error message string is thrown as an exception. For options documentation, see 'man pcre'
Construct from a constant regex to save compilation time.
If you are using the same constant regular expression frequently, you can use this constructor to save the time to compile and study it. First, declare a private constant copy of your expression - this will be compiled by PCRE just once when it is instantiated:
Then in your method, construct a copy of it to use when matching strings:
Constructing this copy does not require a PCRE call to compile the expression.
~RegEx | ( | ) |
int SubStrings | ( | void | ) | const |
Count the number of possible substrings returned by this expression.
The match of the entire expression is also considered a substring, so the return value will always be >= 1.
This method is especially useful when the regular expression is loaded from some external source. For a hard-coded expression, the return is a constant, so you really don't need this method.
bool Search | ( | const char * | subject, |
int | len = -1 , |
||
int | options = 0 |
||
) |
Search a string for matches to this regular expression.
Apply the regular expression to the subject string. Optional parameter len can be used to pass the subject's length to Search(). If not specified (or less than 0), strlen() is used internally to determine the length. Parameter options can contain any combination of options; for options documentation, see 'man pcre'
subject | the string to be searched for a match |
len | the length of the subject string |
options | sum of any PCRE options flags |
bool SearchAt | ( | const char * | subject, |
int | offset, | ||
int | len = -1 , |
||
int | options = 0 |
||
) |
Search a string starting at some offset for matches to this regular expression.
Apply the regular expression to the subject string, starting at the given offset. If the length is not specified, then strlen(subject) is used. Parameter options can contain any combination of options; for options documentation, see 'man pcre'
subject | the string to be searched for a match |
offset | offset to begin search in subject string |
len | the length of the subject string |
options | sum of any PCRE options flags |
bool SearchAgain | ( | int | options = 0 | ) |
Repeat the last search operation, starting immediately after the previous match.
SearchAgain() applies the regular expression to the same subject last passed to Search or SearchAt, but restarts the search after the last match. Subsequent calls to SearchAgain() will find all matches in the subject.
options | sum of any PCRE options flags |
int Matches | ( | ) |
Get the maximum substring value from the most recent search.
May only be called after a successful search using one of the searching interfaces, and applies to the results of that call.
bool MatchString | ( | UtlString * | matched, |
int | i = 0 |
||
) |
Append a match from the last search operation to a UtlString.
May only be called after a successful search and applies to the results of that call.
Example:
would set the UtlStrings
matched | string to append the match to - may be NULL, in which case no string is returned, but the return code still indicates whether or not this substring was matched. |
i | which substring to append from the last search
|
bool Match | ( | const int | i, |
int & | offset, | ||
int & | length | ||
) |
Get the position and length of a match in the subject.
May only be called after a successful call to one of the searching methods, and applies to the results of that call.
Parameter i must be less than SubStrings().
Example:
would set the values
i | input - must be < SubStrings() */ |
offset | output - offset in last subject of the n'th match |
length | output - length in last subject of the n'th match |
int MatchStart | ( | const int | i | ) |
Get the position of a match in the subject.
May only be called after a successful call to one of the searching methods, and applies to the results of that call.
Parameter i must be less than SubStrings().
This is useful when searching at an offset in a string to check whether or not the match was at the offset or somewhere later in the string.
Example:
Note that this is not the same as haveing written the regular expression so that it is anchored: "^A+(B+)(C+)" because the anchor always refers to the actual start of the string (in the example, before the 'x'), even when used with an offset. So the 'result' variable in the example would be true.
i | input - must be < SubStrings() */ |
bool BeforeMatchString | ( | UtlString * | before | ) |
Append string preceeding the most recently matched value to a UtlString.
May only be called after a successful search and applies to the results of that call. This is equivalent to the Perl $` variable.
would set the UtlString getBefore to "xxa".
before | string to append to - may be NULL, in which case no string is returned, but the return code still indicates whether or not there was some string preceeding the last match. |
bool AfterMatchString | ( | UtlString * | before | ) |
Append string following the most recently matched value to a UtlString.
May only be called after a successful search and applies to the results of that call. This is equivalent to the Perl $' variable.
would set the UtlString getAfter to "cyy".
before | string to append to - may be NULL, in which case no string is returned, but the return code still indicates whether or not there was some string following the last match. |
int AfterMatch | ( | int | i | ) |
Get the offset of the first character past the matched value.
May only be called after a successful search and applies to the results of that call.
Example:
would set
i | the substring specifier |
const char * Match | ( | int | i = 0 | ) |
Get a string matched by a previous search.
May only be called after a successful search, and applies to the results of that call. Parameter i must be less than SubStrings().
i | must be < SubStrings() |
|
static |
Default maximum for the recursion depth in searches.
The PCRE internal match() function implements some searches by recursion. This value is the default maximumm allowed depth for that recursion. It can be changed to some other value by passing the maxDepth option argument to the RegEx constructor. It is set at compile time from the SIPX_MAX_REGEX_RECURSION macro, if that value is defined.
If the maximum is exceeded, the match fails.
If this or the maxDepth constructor argument are set to zero, then no limit is enforced (use with caution).
See the discussions of stack size in the pcre documentation.