Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members

RegEx Class Reference

class RegEx, a simple and small API wrapper for PCRE. More...

#include <regexp.h>

List of all members.

Public Member Functions

 RegEx (const string &regex, int options=0)
 Constructor.
 RegEx (const RegEx &y)
 Copy constructor.
RegExoperator= (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


Detailed Description

class RegEx, a simple and small API wrapper for PCRE.

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.

RegEx::~RegEx()

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 & Destructor Documentation

RegEx::RegEx const string &  regex,
int  options = 0
[inline]
 

Constructor.

RegEx::RegEx const RegEx y  )  [inline]
 

Copy constructor.

RegEx::~RegEx  )  [inline]
 

Destructor.


Member Function Documentation

void RegEx::ClearMatchList void   )  [inline, private]
 

Clear match list.

void RegEx::clone const RegEx y  )  [inline, private]
 

auxiliary for copy and assignation constructors

string RegEx::Match int  i = 1  )  [inline]
 

Get match for i-th substring in regexp.

RegEx& RegEx::operator= const RegEx y  )  [inline]
 

Assignation.

bool RegEx::Search const char *  subject,
int  len = -1,
int  options = 0
[inline]
 

Search RE in string.

bool RegEx::SearchAgain int  options = 0  )  [inline]
 

Search RE in string again.

int RegEx::SubStrings void   )  const [inline]
 

Get number of substrings.


Member Data Documentation

const char* RegEx::lastsubject [private]
 

const char* * RegEx::matchlist [private]
 

int* RegEx::ovector [private]
 

pcre_extra* RegEx::pe [private]
 

pcre* RegEx::re [private]
 

int RegEx::slen [private]
 

int RegEx::substrcount [private]
 


The documentation for this class was generated from the following file:
Generated on Wed Apr 26 12:58:50 2006 for FreeLing by  doxygen 1.4.4