【发布时间】:2022-01-11 16:19:58
【问题描述】:
我尝试使用 PCRE 匹配逗号分隔的键值对。文本如下所示:
"key1": "value1", "key2": "value2"
测试的模式是:
/(\s*)(,?)"([a-zA-Z0-9]+?)":(\s*)"([a-zA-Z0-9]+?)"/gm
正则表达式可以测试here:
它可以在这个测试页面中工作,但是使用这个 C 代码它只显示第一个匹配的组。
pcre *re;
pcre_extra *sd;
const char *error;
int rc, erroroffset, i;
int ovector[OVECCOUNT];
re = pcre_compile(pattern, 0, &error, &erroroffset, NULL);
sd = pcre_study(
re, /* result of pcre_compile() */
0, /* no options */
&error); /* set to NULL or points to a message */
rc = pcre_exec( /* see below for details of pcre_exec() options */
re, sd, json, 7, 0, 0, ovector, 30);
pcre_free_study(sd);
printf("Match succeeded at offset %d\n", ovector[0]);
for (i = 0; i < rc; i++) {
char *substring_start = json + ovector[2*i];
int substring_length = ovector[2*i+1] - ovector[2*i];
printf("%2d: '%.*s'\n", i, substring_length, substring_start);
}
结果是
Match succeeded at offset 1
0: '"key1": "value1"'
1: ''
2: ''
3: 'key1'
4: ' '
5: 'value1'
但我需要所有匹配的组,与
'key2'
'value2'
【问题讨论】:
-
您没有在问题中显示正则表达式 - 异地链接可能是有用的辅助信息,但不是问题的一部分。您还需要阅读如何创建 MCVE(Minimal, Complete, Verifiable Example — 或 MRE 或 SO 现在使用的任何名称)或 SSCCE(Short, Self-Contained, Correct Example) — 以不同的名称创建相同的想法。您尚未显示输入数据,但我们不必猜测。
-
更新很有帮助——谢谢。您应该考虑在正则表达式的冒号前添加
\s*; JSON 允许有空格。