C++ Library containing a lot of needful things: Stack Trace, Command Line Parser, Resource Handling, Configuration Files, Unix Command Execution, Directories, Regular Expressions, Tokenizer, Function Trace, Standard Extensions.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.4 KiB

20 years ago
/** @file
$Id$
$Date$
$Author$
@copy © Marc Wäckerlin
@license LGPL, see file <a href="license.html">COPYING</a>
$Log$
Revision 1.2 2004/12/16 13:09:31 marc
possibility to evaluate and extract sub expressions
20 years ago
Revision 1.1 2004/12/14 20:20:30 marc
initial version
*/
#include <mrw/string.hxx>
#include <mrw/regexp.hxx>
#include <mrw/exception.hxx>
20 years ago
namespace mrw {
RegExp::RegExp(const std::string& pattern, bool hassub, int flags)
throw(std::exception):
_hassub(hassub) {
if (flags&nosub) throw mrw::invalid_argument("nosub");
if (regcomp(&_regex, pattern.c_str(), (_hassub?flags:(flags|nosub))))
throw mrw::invalid_argument(pattern);
20 years ago
}
RegExp::~RegExp() throw() {
regfree(&_regex);
}
bool RegExp::operator()(const std::string& text) throw(std::bad_exception) {
if (_hassub)
return !regexec(&_regex, (_text=text).c_str(), MAX_SUB, _sub, 0);
else
return !regexec(&_regex, text.c_str(), 0, 0, 0);
}
std::string RegExp::operator[](unsigned int n) const throw(std::exception) {
if (!_hassub)
throw mrw::invalid_argument("initialized with no sub expressions");
if (n>=MAX_SUB || _sub[n].rm_so<0 || _sub[n].rm_eo<0)
throw mrw::invalid_argument(mrw::string(n));
return _text.substr(_sub[n].rm_so, _sub[n].rm_eo-_sub[n].rm_so);
20 years ago
}
20 years ago
}