54 #include <sys/types.h> 66 #include <rnr/rnrconfig.h> 79 const char *RegEx::ReInvalid =
"inre";
81 RegEx::RegEx(
int nFlags)
89 RegEx::RegEx(
const string &strRegEx,
int nFlags)
91 m_strRegEx = strRegEx;
99 RegEx::RegEx(
const char *sRegEx,
int nFlags)
105 if( (sRegEx != NULL) && (*sRegEx != 0) )
144 RegEx &RegEx::operator=(
const string &rhs)
155 RegEx &RegEx::operator=(
const char *rhs)
159 if( (rhs != NULL) && (*rhs != 0) )
173 bool RegEx::match(
const string &strInput,
int nFlags)
const 179 else if( regexec(&m_regex, strInput.c_str(), 0, NULL, nFlags) == ReOk )
189 bool RegEx::match(
const string &strInput,
int nFlags)
195 setError(ReENotComp);
198 else if( (rc = regexec(&m_regex, strInput.c_str(), 0, NULL, nFlags) == ReOk) )
210 bool RegEx::match(
const char *sInput,
int nFlags)
const 219 return match(strInput, nFlags);
222 bool RegEx::match(
const char *sInput,
int nFlags)
231 return match(strInput, nFlags);
234 size_t RegEx::match(
const string &strInput,
236 const size_t uMaxSubMatches,
237 const int nFlags)
const 239 regmatch_t pos[uMaxSubMatches];
248 return matches.size();
251 sIn = s = strInput.c_str();
254 while( regexec(&m_regex, s, uMaxSubMatches, pos, flags) == ReOk )
256 for(
size_t i = 0; i < uMaxSubMatches; ++i)
260 if( pos[i].rm_so == -1 )
265 m.
m_uStart = pos[i].rm_so + (s - sIn);
266 m.
m_uEnd = pos[i].rm_eo + (s - sIn);
274 matches.push_back(m);
282 return matches.size();
285 size_t RegEx::match(
const string &strInput,
287 const size_t uMaxSubMatches,
290 regmatch_t pos[uMaxSubMatches];
300 setError(ReENotComp);
301 return matches.size();
304 sIn = s = strInput.c_str();
307 while( (rc = regexec(&m_regex, s, uMaxSubMatches, pos, flags)) == ReOk )
309 for(
size_t i = 0; i < uMaxSubMatches; ++i)
313 if( pos[i].rm_so == -1 )
318 m.
m_uStart = pos[i].rm_so + (s - sIn);
319 m.
m_uEnd = pos[i].rm_eo + (s - sIn);
327 matches.push_back(m);
335 if( matches.size() == 0 )
340 return matches.size();
343 size_t RegEx::match(
const char *sInput,
345 const size_t uMaxSubMatches,
346 const int nFlags)
const 355 return match(strInput, matches, uMaxSubMatches, nFlags);
358 size_t RegEx::match(
const char *sInput,
360 const size_t uMaxSubMatches,
370 return match(strInput, matches, uMaxSubMatches, nFlags);
373 bool RegEx::compile()
379 if( m_strRegEx.length() == 0 )
383 else if( (rc = regcomp(&m_regex, m_strRegEx.c_str(), m_nFlags)) == ReOk )
391 LOGERROR(
"'%s': %s", m_strRegEx.c_str(), m_strReError.c_str());
397 void RegEx::setFlags(
const int nFlags)
399 int oldFlags = m_nFlags;
403 if( (m_nFlags != oldFlags) && !m_strRegEx.empty() )
409 void RegEx::groomFlags(
const int nFlags)
411 m_nFlags = nFlags & (ReFlagICase | ReFlagNewLine);
412 m_nFlags |= REG_EXTENDED;
415 void RegEx::setError(
const int nCode)
422 m_strReError.clear();
425 m_strReError =
"No pre-compiled regular expression";
428 m_strReError =
"Not compiled";
434 regerror(m_nReCode, &m_regex, buf,
sizeof(buf));
442 void RegEx::freeReMem()
456 os << RegEx::ReInvalid;
464 for(
size_t i = 0; i < strRe.size(); ++i)
511 while( !is.fail() && !is.eof() && isspace(is.peek()) )
516 while( !bHasRe && !is.fail() && !is.eof() )
520 if( is.fail() || is.eof() )
528 else if( expected != 0 )
549 LOGERROR(
"%s: Got '%c', expected '%c' after '%s'. " 550 "Required format: re\"REGEX\"",
551 LOGFUNCNAME, c, expected, strRe.c_str());
552 cin.setstate(ios::failbit);
581 LOGERROR(
"%s: Unexpected end-of-line after '%s'.",
582 LOGFUNCNAME, strRe.c_str());
583 cin.setstate(ios::failbit);
586 if( isascii(c) && !iscntrl(c) )
592 LOGERROR(
"%s: Unexpected non-ascii character 0x%02x after %s.",
593 LOGFUNCNAME, c, strRe.c_str());
594 cin.setstate(ios::failbit);
603 re = strRe.substr(3);
std::vector< ReMatch > ReMatchVec
vector of matches
size_t m_uEnd
ending index in input where match is found
const std::string & getRegEx() const
Get the pre-compiled regular expression.
int m_nFlags
compile and matching flags
std::ostream & operator<<(std::ostream &os, const LogBook &log)
Stream insertion operator.
size_t m_uStart
starting index in input where match is found
std::string m_strMatch
matching (sub)string
Regular expression match structure.
std::string m_strRegEx
pre-compiled regular expression string
The Regular Expression Class interface.
std::istream & operator>>(std::istream &is, RegEx &re)
Extract from input stream to object.
bool isValid() const
Test if in a valid state (i.e. compiled).