32 #define REGEXP_H__ "deer::RegExp v0.1 Dec 2004 Markus.Peura@fmi.fi"
66 RegExp(
const std::string &str =
"",
int flags = REG_EXTENDED);
77 RegExp &operator=(
const std::string &str);
81 bool setExpression(
const std::string & str,
int flags = REG_EXTENDED);
85 void clear(){ writableResult.clear(); };
89 return !regExpString.empty();
100 return regExpString.empty();
105 void setFlags(
int flags){
113 bool test(
const std::string & str)
const;
130 return execute(str, writableResult);
138 void replace(
const std::string &src,
const std::string &replacement, std::string & dst)
const {
139 std::stringstream sstr;
140 replace(src, replacement, sstr);
145 void replace(
const std::string &src,
const std::string &replacement, std::ostream & sstr)
const;
150 void replace(
const std::string &src,
const std::string ®exp,
const std::string &replacement, std::string & dst){
151 RegExp r(std::string(
"(^.*)?(") + regexp + std::string(
")(.*$)?"));
152 r.
replace(src, replacement, dst);
159 const std::string & toStr()
const {
173 regex_t regExpBinary;
175 std::string regExpString;
180 int expectedMatchCount()
const;
183 std::ostream & operator<<(std::ostream &ostr,
const drain::RegExp & r);
190 const size_t n = regExpBinary.re_nsub + 1;
199 std::vector<regmatch_t> pmatch(n);
208 int resultCode = regexec(®ExpBinary, src.c_str(), pmatch.size(), &pmatch[0], 0) ;
210 if (resultCode != REG_NOMATCH){
211 for (std::vector<regmatch_t>::const_iterator it = pmatch.begin(); it < pmatch.end(); ++it) {
212 const regmatch_t &m = *it;
214 result.push_back(src.substr(m.rm_so, m.rm_eo - m.rm_so));
static void replace(const std::string &src, const std::string ®exp, const std::string &replacement, std::string &dst)
Replaces std::string segment matching search to the given std::string.
Definition: RegExp.h:150
std::vector< std::string > result_t
Native result type, also for external result object.
Definition: RegExp.h:166
bool empty() const
Returns true, if expression is empty.
Definition: RegExp.h:99
const std::vector< std::string > & result
Public interface for the result.
Definition: RegExp.h:107
RegExp(const std::string &str="", int flags=REG_EXTENDED)
Constructor.
Definition: RegExp.cpp:59
int execute(const std::string &str, T &result) const
Apply regexp matching in a string. Returns 0 on success, REG_NOMATCH on failure.
Definition: RegExp.h:187
void replace(const std::string &src, const std::string &replacement, std::string &dst) const
Replaces std::string segment matching search regexp to the given std::string.
Definition: RegExp.h:138
int execute(const std::string &str) const
Variant using internal vector.
Definition: RegExp.h:129
virtual ~RegExp()
Destructor.
Definition: RegExp.cpp:70
bool test(const std::string &str) const
Tests if the regular expression accepts the given std::string.
Definition: RegExp.cpp:128
Definition: DataSelector.cpp:1277