appkit  1.5.1
RoadNarrows Robotics Application Kit
rnr::RegEx Class Reference

Regular Express Class. More...

#include <RegEx.h>

Classes

struct  ReMatch
 Regular expression match structure. More...
 

Public Types

typedef std::vector< ReMatchReMatchVec
 vector of matches
 

Public Member Functions

 RegEx (int nFlags=ReFlagDefaults)
 Default constructor. More...
 
 RegEx (const std::string &strRegEx, int nFlags=ReFlagDefaults)
 String initialization constructor. More...
 
 RegEx (const char *sRegEx, int nFlags=ReFlagDefaults)
 Null-terminated char* initialization constructor. More...
 
 RegEx (const RegEx &src)
 Copy constructor. More...
 
virtual ~RegEx ()
 Default destructor.
 
RegExoperator= (const RegEx &rhs)
 Assignment copy operator. More...
 
RegExoperator= (const std::string &rhs)
 Assignment operator. More...
 
RegExoperator= (const char *rhs)
 Assignment operator. More...
 
bool match (const char *sInput, const int nFlags=ReFlagDefaults)
 Match the input char* against the regular expression. More...
 
bool match (const char *sInput, const int nFlags=ReFlagDefaults) const
 
size_t match (const std::string &strInput, ReMatchVec &matches, const size_t uMaxSubMatches=ReMaxSubMatchesDft, const int nFlags=ReFlagDefaults)
 Find all substrings in the input that match the regular expression. More...
 
size_t match (const std::string &strInput, ReMatchVec &matches, const size_t uMaxSubMatches=ReMaxSubMatchesDft, const int nFlags=ReFlagDefaults) const
 
size_t match (const char *sInput, ReMatchVec &matches, const size_t uMaxSubMatches=ReMaxSubMatchesDft, const int nFlags=ReFlagDefaults)
 Find all substrings in input that match the regular expression. More...
 
size_t match (const char *sInput, ReMatchVec &matches, const size_t uMaxSubMatches=ReMaxSubMatchesDft, const int nFlags=ReFlagDefaults) const
 
const std::string & getRegEx () const
 Get the pre-compiled regular expression. More...
 
bool isValid () const
 Test if in a valid state (i.e. compiled). More...
 
int getFlags () const
 Get compile behavior flags. More...
 
void setFlags (int nFlags)
 Set new compile behavior flags. More...
 
int getReturnCode () const
 Get the extened return code from the last RegEx operation. More...
 
const std::string & getErrorStr () const
 Get the last RegExs operation error string. More...
 
bool match (const std::string &strInput, const int nFlags=ReFlagDefaults)
 Match the input string against the regular expression. More...
 
bool match (const std::string &strInput, const int nFlags=ReFlagDefaults) const
 

Static Public Attributes

static const int ReOk = REG_NOERROR
 Special return and error codes. More...
 
static const int ReENoExpr = -1000
 no pre-compiled express
 
static const int ReENotComp = -1001
 not compiled
 
static const int ReFlagDefaults = 0
 Regular expression compile and matching behavior flags. More...
 
static const int ReFlagICase = REG_ICASE
 ignore case when matching
 
static const int ReFlagNewLine = REG_NEWLINE
 
force bol/eol matching
 
static const int ReFlagNotBoL = REG_NOTBOL
 input not begin of line
 
static const int ReFlagNotEoL = REG_NOTEOL
 input not end of line
 
static const char * ReInvalid = "inre"
 Concerning, in reality, in regards to invalid regular expressions.
 
static const size_t ReMaxSubMatchesDft = 32
 

Protected Member Functions

bool compile ()
 Compile the regular expression string. More...
 
void groomFlags (const int nFlags)
 Groom compile behavior flags, disabling any unsupported flags. More...
 
void setError (const int nCode)
 Set the error code and associated error string. More...
 
void freeReMem ()
 Free compiled regular expression memory.
 

Protected Attributes

std::string m_strRegEx
 pre-compiled regular expression string
 
int m_nFlags
 compile and matching flags
 
bool m_bIsValid
 expression [not] successfully compiled
 
regex_t m_regex
 compiled reqular expression
 
int m_nReCode
 compiled regular expression return code
 
std::string m_strReError
 compiled regualar expresson error string
 

Friends

std::ostream & operator<< (std::ostream &os, const RegEx &re)
 Insert object into output stream. More...
 
std::istream & operator>> (std::istream &is, RegEx &re)
 Extract from input stream to object. More...
 

Detailed Description

Regular Express Class.

Regular expression are evaluated using the POSIX Extended Regular Expression syntax.

Definition at line 84 of file RegEx.h.

Constructor & Destructor Documentation

RegEx::RegEx ( int  nFlags = ReFlagDefaults)

Default constructor.

No regular expression compile will be attempted.

Parameters
nFlagsBitwise-or of compile behavior flags.

Definition at line 81 of file RegEx.cxx.

82 {
83  m_bIsValid = false;
84 
85  groomFlags(nFlags);
87 }
void setError(const int nCode)
Set the error code and associated error string.
Definition: RegEx.cxx:415
static const int ReENoExpr
no pre-compiled express
Definition: RegEx.h:94
void groomFlags(const int nFlags)
Groom compile behavior flags, disabling any unsupported flags.
Definition: RegEx.cxx:409
bool m_bIsValid
expression [not] successfully compiled
Definition: RegEx.h:415
rnr::RegEx::RegEx ( const std::string &  strRegEx,
int  nFlags = ReFlagDefaults 
)

String initialization constructor.

Regular expression compile will be attempted.

Parameters
strRegExPre-compile regular expression string.
nFlagsBitwise-or of compile behavior flags.
RegEx::RegEx ( const char *  sRegEx,
int  nFlags = ReFlagDefaults 
)

Null-terminated char* initialization constructor.

If not NULL or empty, regular expression compile will be attempted.

Parameters
sRegExNull-terminated pre-compile regular expression string.
nFlagsBitwise-or of compile behavior flags.

Definition at line 99 of file RegEx.cxx.

100 {
101  m_bIsValid = false;
102 
103  groomFlags(nFlags);
104 
105  if( (sRegEx != NULL) && (*sRegEx != 0) )
106  {
107  m_strRegEx = sRegEx;
108  compile();
109  }
110  else
111  {
113  }
114 }
void setError(const int nCode)
Set the error code and associated error string.
Definition: RegEx.cxx:415
static const int ReENoExpr
no pre-compiled express
Definition: RegEx.h:94
void groomFlags(const int nFlags)
Groom compile behavior flags, disabling any unsupported flags.
Definition: RegEx.cxx:409
std::string m_strRegEx
pre-compiled regular expression string
Definition: RegEx.h:413
bool m_bIsValid
expression [not] successfully compiled
Definition: RegEx.h:415
bool compile()
Compile the regular expression string.
Definition: RegEx.cxx:373
RegEx::RegEx ( const RegEx src)

Copy constructor.

Regular expression compile will be attempted.

Parameters
srcSource object.

Definition at line 116 of file RegEx.cxx.

References m_nFlags, and m_strRegEx.

117 {
118  m_strRegEx = src.m_strRegEx;
119  m_bIsValid = false;
120 
121  groomFlags(src.m_nFlags);
122 
123  compile();
124 }
int m_nFlags
compile and matching flags
Definition: RegEx.h:414
void groomFlags(const int nFlags)
Groom compile behavior flags, disabling any unsupported flags.
Definition: RegEx.cxx:409
std::string m_strRegEx
pre-compiled regular expression string
Definition: RegEx.h:413
bool m_bIsValid
expression [not] successfully compiled
Definition: RegEx.h:415
bool compile()
Compile the regular expression string.
Definition: RegEx.cxx:373

Member Function Documentation

bool RegEx::compile ( )
protected

Compile the regular expression string.

Returns
Returns true success, false otherwise. On failure, check the reason with getReturnCode() and/or getErrorStr().

Definition at line 373 of file RegEx.cxx.

374 {
375  int rc; // return code
376 
377  freeReMem();
378 
379  if( m_strRegEx.length() == 0 )
380  {
382  }
383  else if( (rc = regcomp(&m_regex, m_strRegEx.c_str(), m_nFlags)) == ReOk )
384  {
385  m_bIsValid = true;
386  setError(ReOk); // not an error
387  }
388  else
389  {
390  setError(rc);
391  LOGERROR("'%s': %s", m_strRegEx.c_str(), m_strReError.c_str());
392  }
393 
394  return m_bIsValid;
395 }
static const int ReOk
Special return and error codes.
Definition: RegEx.h:93
void setError(const int nCode)
Set the error code and associated error string.
Definition: RegEx.cxx:415
int m_nFlags
compile and matching flags
Definition: RegEx.h:414
void freeReMem()
Free compiled regular expression memory.
Definition: RegEx.cxx:442
std::string m_strReError
compiled regualar expresson error string
Definition: RegEx.h:418
regex_t m_regex
compiled reqular expression
Definition: RegEx.h:416
static const int ReENoExpr
no pre-compiled express
Definition: RegEx.h:94
std::string m_strRegEx
pre-compiled regular expression string
Definition: RegEx.h:413
bool m_bIsValid
expression [not] successfully compiled
Definition: RegEx.h:415
const std::string& rnr::RegEx::getErrorStr ( ) const
inline

Get the last RegExs operation error string.

Returns
Error string.

Definition at line 401 of file RegEx.h.

References m_strReError, operator<<, and operator>>.

Referenced by rnr::cmd::CommandLine::parseVarRegExpr(), utPr(), and utRun().

402  {
403  return m_strReError;
404  }
std::string m_strReError
compiled regualar expresson error string
Definition: RegEx.h:418
int rnr::RegEx::getFlags ( ) const
inline

Get compile behavior flags.

Returns
Bitwise-or of compile behavior flags.

Definition at line 364 of file RegEx.h.

References m_nFlags, and setFlags().

Referenced by utPr().

365  {
366  return m_nFlags;
367  }
int m_nFlags
compile and matching flags
Definition: RegEx.h:414
const std::string& rnr::RegEx::getRegEx ( ) const
inline

Get the pre-compiled regular expression.

Returns
If a regular expressing exist, then the expression string is returned. Else NULL is returned.

Definition at line 342 of file RegEx.h.

References m_strRegEx.

Referenced by freeReMem(), rnr::cmd::CmdArgDef::getRegEx(), rnr::cmd::CommandLine::parseVarRegExpr(), and utPr().

343  {
344  return m_strRegEx;
345  }
std::string m_strRegEx
pre-compiled regular expression string
Definition: RegEx.h:413
int rnr::RegEx::getReturnCode ( ) const
inline

Get the extened return code from the last RegEx operation.

Return Code Description
ReOk (REG_NOERROR) No error
REG_NOMATCH Input does no match regular expression
REG_BAD*, REG_E* The regex C interface standard compile errors
ReENoExpr No regular expression extended error
ReENotComp Not compiled extended error
Returns
nCode Extended return code.

Definition at line 391 of file RegEx.h.

References m_nReCode.

Referenced by utPr().

392  {
393  return m_nReCode;
394  }
int m_nReCode
compiled regular expression return code
Definition: RegEx.h:417
void RegEx::groomFlags ( const int  nFlags)
protected

Groom compile behavior flags, disabling any unsupported flags.

Parameters
nFlagsBitwise-or of compile behavior flags.

Definition at line 409 of file RegEx.cxx.

410 {
411  m_nFlags = nFlags & (ReFlagICase | ReFlagNewLine);
412  m_nFlags |= REG_EXTENDED;
413 }
static const int ReFlagICase
ignore case when matching
Definition: RegEx.h:107
int m_nFlags
compile and matching flags
Definition: RegEx.h:414
static const int ReFlagNewLine
force bol/eol matching
Definition: RegEx.h:108
bool rnr::RegEx::isValid ( ) const
inline

Test if in a valid state (i.e. compiled).

If valid, match operation may be applied.

Returns
Returns true or false;

Definition at line 354 of file RegEx.h.

References m_bIsValid.

Referenced by freeReMem(), rnr::cmd::CommandLine::parseVarRegExpr(), and utPr().

355  {
356  return m_bIsValid;
357  }
bool m_bIsValid
expression [not] successfully compiled
Definition: RegEx.h:415
bool rnr::RegEx::match ( const std::string &  strInput,
const int  nFlags = ReFlagDefaults 
)

Match the input string against the regular expression.

The entire input must match the regular expression.

Parameters
[in]strInputInput string to match.
Returns
Returns true if a match, false otherwise. For the non-constant version, an error is set if no match.

Referenced by utConstRe(), and utRun().

bool RegEx::match ( const char *  sInput,
const int  nFlags = ReFlagDefaults 
)

Match the input char* against the regular expression.

The entire input must match the regular expression.

Parameters
[in]sInputNull-terminated input char* to match.
Returns
Returns true if a match, false otherwise. For the non-constant version, an error is set if no match.

Definition at line 222 of file RegEx.cxx.

References rnr::RegEx::ReMatch::m_strMatch, rnr::RegEx::ReMatch::m_uEnd, and rnr::RegEx::ReMatch::m_uStart.

223 {
224  string strInput;
225 
226  if( sInput != NULL )
227  {
228  strInput = sInput;
229  }
230 
231  return match(strInput, nFlags);
232 }
bool match(const std::string &strInput, const int nFlags=ReFlagDefaults)
Match the input string against the regular expression.
size_t rnr::RegEx::match ( const std::string &  strInput,
ReMatchVec matches,
const size_t  uMaxSubMatches = ReMaxSubMatchesDft,
const int  nFlags = ReFlagDefaults 
)

Find all substrings in the input that match the regular expression.

As an example, the following C++ code snippet:

rnr::RegEx re("[A-Z][a-z ]*[ ]+(cat)[ ]+[a-z ]+[ ]+(dog)[a-z ]*\.");
std::string input("My cat is not a dog. But my cat has dog breath.");
re.match(input, matches, 4);
for(size_t i = 0; i < matches.size(); ++i)
{
std::cout << i << ". (" << matches[i].m_uStart << ","
<< matches[i].m_uEnd << ") '"
<< matches[i].m_strMatch << "'" << std::endl;
}

Produces the output:

0. (0,19) 'My cat is not a dog.'
1. (3,5) 'cat'
2. (16,18) 'dog'
3. (21,46) 'But my cat has dog breath.'
4. (28,30) 'cat'
5. (36,38) 'dog'

Notes:

  • The match start and end values are indices into the original input string, marking the location of the matched string.
  • In the call match(), the maximum number of specified submatches is 4, but the number of matches found is 6. This is because 2 entire matches occurred, listed on output 0 and 3. Each occurance matched 3 (sub)strings, which is less than the 4 maximum.
Parameters
[in]strInputInput string.
[in,out]Vectorof matches.
uMaxSubMatchesMaximum number of submatches per match.
nFlagsBitwise-or of matching behavior flags.
Returns
Returns number of matches. For the non-constant version, an error is set if no match.
size_t RegEx::match ( const char *  sInput,
ReMatchVec matches,
const size_t  uMaxSubMatches = ReMaxSubMatchesDft,
const int  nFlags = ReFlagDefaults 
)

Find all substrings in input that match the regular expression.

See also
See above for description.
Parameters
[in]sInputNull-terminated input char*.
[in,out]Vectorof matches.
uMaxSubMatchesMaximum number of submatches per match.
nFlagsBitwise-or of matching behavior flags.
Returns
Returns number of matches. For the non-constant version, an error is set if no match.

Definition at line 358 of file RegEx.cxx.

362 {
363  string strInput;
364 
365  if( sInput != NULL )
366  {
367  strInput = sInput;
368  }
369 
370  return match(strInput, matches, uMaxSubMatches, nFlags);
371 }
bool match(const std::string &strInput, const int nFlags=ReFlagDefaults)
Match the input string against the regular expression.
RegEx & RegEx::operator= ( const RegEx rhs)

Assignment copy operator.

Regular expression compile will be attempted.

Parameters
rhsRegular expression class object.
Returns
This regular expression object.

Definition at line 131 of file RegEx.cxx.

References m_nFlags, and m_strRegEx.

132 {
133  freeReMem();
134 
135  m_strRegEx = rhs.m_strRegEx;
136 
137  groomFlags(rhs.m_nFlags);
138 
139  compile();
140 
141  return *this;
142 }
int m_nFlags
compile and matching flags
Definition: RegEx.h:414
void freeReMem()
Free compiled regular expression memory.
Definition: RegEx.cxx:442
void groomFlags(const int nFlags)
Groom compile behavior flags, disabling any unsupported flags.
Definition: RegEx.cxx:409
std::string m_strRegEx
pre-compiled regular expression string
Definition: RegEx.h:413
bool compile()
Compile the regular expression string.
Definition: RegEx.cxx:373
RegEx& rnr::RegEx::operator= ( const std::string &  rhs)

Assignment operator.

Regular expression compile will be attempted.

Parameters
rhsString regular expression.
Returns
This regular expression object.
RegEx & RegEx::operator= ( const char *  rhs)

Assignment operator.

If not NULL or empty, regular expression compile will be attempted.

Parameters
rhsNull-terminated string expression object.
Returns
This regular expression object.

Definition at line 155 of file RegEx.cxx.

156 {
157  freeReMem();
158 
159  if( (rhs != NULL) && (*rhs != 0) )
160  {
161  m_strRegEx = rhs;
162  compile();
163  }
164  else
165  {
166  m_strRegEx.clear();
168  }
169 
170  return *this;
171 }
void setError(const int nCode)
Set the error code and associated error string.
Definition: RegEx.cxx:415
void freeReMem()
Free compiled regular expression memory.
Definition: RegEx.cxx:442
static const int ReENoExpr
no pre-compiled express
Definition: RegEx.h:94
std::string m_strRegEx
pre-compiled regular expression string
Definition: RegEx.h:413
bool compile()
Compile the regular expression string.
Definition: RegEx.cxx:373
void RegEx::setError ( const int  nCode)
protected

Set the error code and associated error string.

Parameters
nCodeExtended return code.

Definition at line 415 of file RegEx.cxx.

416 {
417  m_nReCode = nCode;
418 
419  switch( m_nReCode )
420  {
421  case ReOk:
422  m_strReError.clear();
423  break;
424  case ReENoExpr:
425  m_strReError = "No pre-compiled regular expression";
426  break;
427  case ReENotComp:
428  m_strReError = "Not compiled";
429  break;
430  default:
431  {
432  char buf[256];
433 
434  regerror(m_nReCode, &m_regex, buf, sizeof(buf));
435 
436  m_strReError = buf;
437  }
438  break;
439  }
440 }
static const int ReOk
Special return and error codes.
Definition: RegEx.h:93
static const int ReENotComp
not compiled
Definition: RegEx.h:95
std::string m_strReError
compiled regualar expresson error string
Definition: RegEx.h:418
regex_t m_regex
compiled reqular expression
Definition: RegEx.h:416
static const int ReENoExpr
no pre-compiled express
Definition: RegEx.h:94
int m_nReCode
compiled regular expression return code
Definition: RegEx.h:417
void RegEx::setFlags ( int  nFlags)

Set new compile behavior flags.

Note
May result in a new regular expression re-compile.
Parameters
nFlagsBitwise-or of compile behavior flags.

Definition at line 397 of file RegEx.cxx.

Referenced by getFlags(), and rnr::cmd::CommandLine::parseVarRegExpr().

398 {
399  int oldFlags = m_nFlags;
400 
401  groomFlags(nFlags);
402 
403  if( (m_nFlags != oldFlags) && !m_strRegEx.empty() )
404  {
405  compile();
406  }
407 }
int m_nFlags
compile and matching flags
Definition: RegEx.h:414
void groomFlags(const int nFlags)
Groom compile behavior flags, disabling any unsupported flags.
Definition: RegEx.cxx:409
std::string m_strRegEx
pre-compiled regular expression string
Definition: RegEx.h:413
bool compile()
Compile the regular expression string.
Definition: RegEx.cxx:373

Friends And Related Function Documentation

std::ostream& operator<< ( std::ostream &  os,
const RegEx re 
)
friend

Insert object into output stream.

Output: re"REGEX" string where REGEX is the regular expression pattern.

Parameters
osOutput stream.
reObject to insert.
Returns
Reference to output stream.

Referenced by getErrorStr().

std::istream& operator>> ( std::istream &  is,
RegEx re 
)
friend

Extract from input stream to object.

Input: re"REGEX" string where REGEX is the regular expression pattern.

Parameters
isInput stream.
reObject to extract into.
Returns
Reference to input stream.

Referenced by getErrorStr().

Member Data Documentation

const int rnr::RegEx::ReFlagDefaults = 0
static

Regular expression compile and matching behavior flags.

See regex(3) for further description of flags.

Flags are a bitwise-or of zero or more of these values.default flags

Definition at line 104 of file RegEx.h.

const size_t rnr::RegEx::ReMaxSubMatchesDft = 32
static

default maximum submatches per match

Definition at line 130 of file RegEx.h.

const int rnr::RegEx::ReOk = REG_NOERROR
static

Special return and error codes.

Note
The error codes must to be 'out-of-band' from the the standard regex REG_* codes. Large negative values should do the trick.regex operation success

Definition at line 93 of file RegEx.h.


The documentation for this class was generated from the following files: