在C代码中具有以下匹配模式:
static const char my_pattern[] = "([0-9]{20})[\\^]";
和以下字符串:
12345678901234567890 ^ otherstrings ^和^ digits12345678901234567890
我想只匹配:
12345678901234567890
为什么我的结果是:
12345678901234567890 ^
谢谢!
编辑:
完整代码示例:
#include <stdio.h>
#include <string.h>
#include <pcre.h>
static const char my_pattern[] = "([0-9]{20})[\\^]";
static pcre* my_pcre = NULL;
static pcre_extra* my_pcre_extra = NULL;
void my_match(const char* src)
{
printf("src: %s\n", src);
int ovector[30]={0};
int retex = pcre_exec(my_pcre, NULL, src, strlen(src), 0, 0, ovector, 30);
if (retex == PCRE_ERROR_NOMATCH){
printf("None match.\n");
}
else{
printf("Matches %d\n", retex);
const char *sp = NULL;
pcre_get_substring((const char *)src, ovector, retex, 0, &sp);
printf("%s\n", sp);
pcre_free_substring(sp);
}
return;
}
int main()
{
const char* err;
int erroffset;
my_pcre = pcre_compile(my_pattern, PCRE_CASELESS, &err, &erroffset, NULL);
my_pcre_extra = pcre_study(my_pcre, 0, &err);
my_match("12345678901234567890^otherstrings^and^digits12345678901234567890");
return 0;
}
答案 0 :(得分:2)
PCRE捕获成对出现在输出向量中。您的完全匹配位于第一对,子组捕获位于第二对。见下文:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pcre.h>
static const char subject[] =
"12345678901234567890^otherstrings^and^digits12345678901234567890";
#define OVSIZE 9
int main()
{
static const char my_pattern[] = "([0-9]{20})[\\^]";
const char *errtext = NULL;
int errofs = 0;
pcre* re = pcre_compile(my_pattern, 0, &errtext, &errofs, NULL);
if (re != NULL)
{
int ov[OVSIZE];
int rc = pcre_exec(re, NULL, subject, sizeof(subject), 0, 0, ov, OVSIZE);
if (rc >= 0)
{
for (int i=0; i<rc; ++i)
printf("group %d: %.*s\n", i, ov[2*i+1]-ov[2*i], subject+ov[2*i]);
}
}
return 0;
}
<强>输出强>
group 0: 12345678901234567890^
group 1: 12345678901234567890
答案 1 :(得分:1)
这是我正在寻找的REGEX模式:
([0-9]{20})(?=\\^)
或更优雅:
([\\d]{20})(?=\\^)