我正在研究一种分析strace输出的解析器。为了获得每个系统调用的对象表示,我需要使用正则表达式解析文件。
所以,我使用带有PCRE规则的regex.h,但是我不能用它来获取子串,我不知道为什么。
例如,我必须解析这一行:
open(“/ etc / ld.so.cache”,O_RDONLY | O_CLOEXEC)= 3
获取:
然后,我写了这个模式:#define _R_READ "open\\(\"([^\"]*)\",[ ]([^)]*)\\)"
但是当我使用这个函数时它不匹配并且不返回子串:
int Parser::match_regex (regex_t * r, const char * to_match)
{
/* "P" is a pointer into the string which points to the end of the
previous match. */
const char * p = to_match;
/* "N_matches" is the maximum number of matches allowed. */
const int n_matches = 10;
/* "M" contains the matches found. */
regmatch_t m[n_matches];
while (1) {
int i = 0;
int nomatch = regexec (r, p, n_matches, m, 0);
if (nomatch) {
printf ("No more matches.\n");
return nomatch;
}
for (i = 0; i < n_matches; i++) {
int start;
int finish;
if (m[i].rm_so == -1) {
break;
}
start = m[i].rm_so + (p - to_match);
finish = m[i].rm_eo + (p - to_match);
if (i == 0) {
printf ("$& is ");
}
else {
printf ("$%d is ", i);
}
printf ("'%.*s' (bytes %d:%d)\n", (finish - start),
to_match + start, start, finish);
}
p += m[0].rm_eo;
}
return 0;
你有什么想法吗?
干杯。