c - PCRE - 偏移 vector ,3 的倍数?

标签 c regex pcre

我正在学习 PCRE,我不明白为什么偏移 vector 必须是 3 的倍数。这是来自 pcredemo.c(rcpcre_exec() 的结果):

/* The output vector wasn't big enough */

if (rc == 0) {
    rc = OVECCOUNT / 3;
    printf("ovector only has room for %d captured substrings\n", rc - 1);
}

/* Show substrings stored in the output vector by number. Obviously, in a real
 * application you might want to do things other than print them. */

for (i = 0; i < rc; i++) {
    char *substring_start = subject + ovector[2 * i];
    int substring_length = ovector[2 * i + 1] - ovector[2 * i];
    printf("%2d: %.*s\n", i, substring_length, substring_start);
}

在我看来,ovector 存储 str1_start, str1_end, str2_start, str2_end, ... ,所以数组可以容纳 OVECCOUNT/2 个字符串。为什么是 OVECCOUNT/3?

谢谢。

最佳答案

The manual :

The first two-thirds of the vector is used to pass back captured substrings, each substring using a pair of integers. The remaining third of the vector is used as workspace by pcre_exec() while matching capturing subpatterns, and is not available for passing back information. The number passed in ovecsize should always be a multiple of three. If it is not, it is rounded down.

关于c - PCRE - 偏移 vector ,3 的倍数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11993966/

相关文章:

ios - 创建IOS 3D效果

ios - RegexKitLite iOS : Password validation - must include one number

php - 正则表达式散列和冒号

regex - Grep/Regex 跨多行固定模式匹配,匹配时有特殊条件

c# - RegEx 字表现 :\w vs [a-zA-Z0-9_]

c - printf格式错误

c++ - 在C++应用程序中嵌入Lua脚本会把Lua部分编译成机器码吗?

c++ - 防止 Hook dll加载

java - 替换字符串中的数字

php - 如果重复在括号中,则可变长度后视编译。为什么?