Stefan wrote:
regex(3) (regcomp, regexec, ...) supports substring match as well,
it's just not enabled in the code. in AmUtils
read_regex_mapping/run_regex_mapping remove REG_NOSUB from the cflags,
run regexec with nmatch and pmatch, and use the results to replace \n
in result.
i removed REG_NOSUB flag from regcomp and modified run_regex_mapping as
follows:
#define MAX_GROUPS 6
bool run_regex_mapping(const RegexMappingVector& mapping, const char* test_s,
string& result) {
regmatch_t groups[MAX_GROUPS];
for (RegexMappingVector::const_iterator it = mapping.begin();
it != mapping.end(); it++) {
if (!regexec(&it->first, test_s, MAX_GROUPS, groups, 0)) {
INFO("match of '%s' to %s\n", test_s, it->second.c_str());
result = it->second;
unsigned int g = 0;
for (g = 1; g < MAX_GROUPS; g++) {
if (groups[g].rm_so == (int)(size_t)-1) break;
DBG("group %u: [%2u-%2u]: %.*s\n",
g, groups[g].rm_so, groups[g].rm_eo,
groups[g].rm_eo - groups[g].rm_so, test_s + groups[g].rm_so);
std::size_t found = result.find("\\" + int2str(g));
if (found != std::string::npos)
result.replace(found, 2, test_s + groups[g].rm_so,
groups[g].rm_eo - groups[g].rm_so);
}
return true;
}
}
return false;
}
based on a few tests, it looks like \1, \2, ... \6 are correctly
replaced in result with corresponding () matches.
this code has two limitations:
1) if result has more than one \n with same 'n', only first of them is
replaced
2) \n is replaced even if it is preceded by another \.
do people think that it is worth including this kind of partially
implemented replacement functionality?
-- juha
_______________________________________________
Semsdev mailing list
[email protected]
http://lists.iptel.org/mailman/listinfo/semsdev