pcre匹配C中的所有组
我想使用PCRE C库递归地匹配一个组 e、 g rc为-1 如何匹配所有组,使匹配项为“5”、“6”、“3”、“2” 作为类比,PHP的pcre匹配C中的所有组,c,regex,pcre,C,Regex,Pcre,我想使用PCRE C库递归地匹配一个组 e、 g rc为-1 如何匹配所有组,使匹配项为“5”、“6”、“3”、“2” 作为类比,PHP的preg\u match\u all解析整个字符串,直到主题结束…尝试以下方法: pcre *myregexp; const char *error; int erroroffset; int offsetcount; int offsets[(0+1)*3]; // (max_capturing_groups+1)*3 myregexp = pcre_com
preg\u match\u all
解析整个字符串,直到主题结束…尝试以下方法:
pcre *myregexp;
const char *error;
int erroroffset;
int offsetcount;
int offsets[(0+1)*3]; // (max_capturing_groups+1)*3
myregexp = pcre_compile("\\d,", 0, &error, &erroroffset, NULL);
if (myregexp != NULL) {
offsetcount = pcre_exec(myregexp, NULL, subject, strlen(subject), 0, 0, offsets, (0+1)*3);
while (offsetcount > 0) {
// match offset = offsets[0];
// match length = offsets[1] - offsets[0];
if (pcre_get_substring(subject, &offsets, offsetcount, 0, &result) >= 0) {
// Do something with match we just stored into result
}
offsetcount = pcre_exec(myregexp, NULL, subject, strlen(subject), 0, offsets[1], offsets, (0+1)*3);
}
} else {
// Syntax error in the regular expression at erroroffset
}
我相信这些评论是不言自明的?试试这个:
pcre *myregexp;
const char *error;
int erroroffset;
int offsetcount;
int offsets[(0+1)*3]; // (max_capturing_groups+1)*3
myregexp = pcre_compile("\\d,", 0, &error, &erroroffset, NULL);
if (myregexp != NULL) {
offsetcount = pcre_exec(myregexp, NULL, subject, strlen(subject), 0, 0, offsets, (0+1)*3);
while (offsetcount > 0) {
// match offset = offsets[0];
// match length = offsets[1] - offsets[0];
if (pcre_get_substring(subject, &offsets, offsetcount, 0, &result) >= 0) {
// Do something with match we just stored into result
}
offsetcount = pcre_exec(myregexp, NULL, subject, strlen(subject), 0, offsets[1], offsets, (0+1)*3);
}
} else {
// Syntax error in the regular expression at erroroffset
}
我相信这些评论是不言自明的?我使用strtok的任何方式,因为“,”在每组后面重复
欢迎使用pcre解决方案 我使用strtok的任何方式,因为“,”在每组之后重复
欢迎使用pcre解决方案 在
pcre\u exec()
上循环,在每次迭代后适当地调整起始位置。是否无法一次性获得它?除非编写函数为您执行循环。在pcre\u exec()
上循环,每次迭代后适当调整起始位置。是否无法一次性获得它?除非编写函数为您执行循环。@Rahul您使用的是哪个版本的PCRE?@FailedDev我尝试过的代码,我在代码的while循环中找到了函数PCRE_exec(),它的参数为偏移量[1]他的命令不对。所以我改变了它:offsetcount=pcre_exec(myregexp,NULL,subject,strlen(subject),offset[1],0,offset,(0+1)*3)代码>那么它工作得很好。@hession可能是不同版本的pcre库。很高兴它的措辞是:)@Rahul您使用的是哪个版本的PCRE?@FailedDev您的代码我已经尝试过了,我在您的代码的while循环中发现了函数PCRE_exec(),它的偏移量[1]的参数顺序不正确。所以我改变了它:offsetcount=pcre_exec(myregexp,NULL,subject,strlen(subject),offset[1],0,offset,(0+1)*3)代码>那么它工作得很好。@hession可能是不同版本的pcre库。很高兴它的措辞是:)