#include <regexp.h>
Public Member Functions | |
RegEx (const string ®ex, int options=0) | |
Constructor. | |
RegEx (const RegEx &y) | |
Copy constructor. | |
RegEx & | operator= (const RegEx &y) |
Assignation. | |
~RegEx () | |
Destructor. | |
int | SubStrings (void) const |
Get number of substrings. | |
bool | Search (const char *subject, int len=-1, int options=0) |
Search RE in string. | |
bool | SearchAgain (int options=0) |
Search RE in string again. | |
string | Match (int i=1) |
Get match for i-th substring in regexp. | |
Private Member Functions | |
void | clone (const RegEx &y) |
auxiliary for copy and assignation constructors | |
void | ClearMatchList (void) |
Clear match list. | |
Private Attributes | |
pcre * | re |
pcre_extra * | pe |
int | substrcount |
int * | ovector |
const char * | lastsubject |
int | slen |
const char ** | matchlist |
RegEx::RegEx(string regex, int options = 0)
The constructor's first parameter is the regular expression the created object shall implement. Optional parameter options can be any combination of PCRE options accepted by pcre_compile(). If compiling the regular expression fails, an error message string is thrown as an exception.
The destructor frees all resources held by the RegEx object.
int RegEx::SubStrings(void) const
Method SubStrings() returns the number of substrings defined by the regular expression. The match of the entire expression is also considered a substring, so the return value will always be >= 1.
bool RegEx::Search(const char * subject, int len = -1, int options = 0)
Method Search() applies the regular expression to parameter subject. 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 PCRE_ANCHORED, PCRE_NOTBOL, PCRE_NOTEOL. PCRE_NOTEMPTY. Search() returns true if a match is found.
bool RegEx::SearchAgain(int options = 0)
SearchAgain() again applies the regular expression to parameter subject last passed to a successful call of Search(). It returns true if a further match is found. Subsequent calls to SearchAgain() will find all matches in subject. Example:
if (Pattern.Search(astring)) { do { printf("%s\n", Pattern.Match()); } while (Pattern.SearchAgain()); }
Parameter options is interpreted as for method Search().
string RegEx::Match(int i = 1)
Method Match() returns a pointer to the matched substring specified with parameter i. Match() may only be called after a successful call to Search() or SearchAgain() and applies to that last Search()/SearchAgain() call. Parameter i must be less than SubStrings(). Match(-1) returns the last searched subject. Match(0) returns the match of the complete regular expression. Match(1) returns $1, etc.
The bottom of this file contains an example using class RegEx. It's the simplest version of grep I could come with. You can compile it by defining REGEX_DEMO on the compiler command line.
|
Constructor.
|
|
Copy constructor.
|
|
Destructor.
|
|
Clear match list.
|
|
auxiliary for copy and assignation constructors
|
|
Get match for i-th substring in regexp.
|
|
Assignation.
|
|
Search RE in string.
|
|
Search RE in string again.
|
|
Get number of substrings.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|