23 #include "RegexUtil.h"
25 map<string, regex_t> RegexUtil::patterns;
27 RegexUtil::RegexUtil() {
32 RegexUtil::~RegexUtil() {
36 vector<string> RegexUtil::search(
const string& text,
const string& pattern) {
41 if(patterns.find(pattern)!=patterns.end())
43 regex = patterns[pattern];
48 reti = regcomp(®ex, pattern.c_str(), REG_EXTENDED);
51 cout <<
"Could not compile regex\n" << endl;
55 patterns[pattern] = regex;
59 reti = regexec(®ex, ttext.c_str(), 1, &pm, 0);
65 match = ttext.substr(pm.rm_so, pm.rm_eo-pm.rm_so);
70 ttext = ttext.substr(pm.rm_eo);
73 reti = regexec (®ex, ttext.c_str(), 1, &pm, 0);
78 string RegexUtil::replace(
const string& text,
const string& pattern,
const string& with) {
83 if(patterns.find(pattern)!=patterns.end())
85 regex = patterns[pattern];
90 reti = regcomp(®ex, pattern.c_str(), REG_EXTENDED);
93 cout <<
"Could not compile regex\n" << endl;
97 patterns[pattern] = regex;
101 reti = regexec(®ex, ttext.c_str(), 1, &pm, 0);
107 match = ttext.substr(pm.rm_so, pm.rm_eo-pm.rm_so);
108 rettxt += ttext.substr(0, pm.rm_so) + with;
113 ttext = ttext.substr(pm.rm_eo);
116 reti = regexec (®ex, ttext.c_str(), 1, &pm, 0);
118 if(ttext!=
"")rettxt += ttext;