c - C regexec 找不到与简单贪婪正则表达式的最长匹配
问题描述
我正在尝试m*
匹配immunity
. 我阅读了 POSIXregex.h
手册页并写了以下内容:
#include <stdio.h>
#include <regex.h>
#define DIE(condition, msg) do { \
if (condition) { \
puts(msg); \
return 1; \
} \
} while (0)
int main(void) {
regex_t preg;
regmatch_t match;
DIE (regcomp(&preg, "m*", REG_EXTENDED), "Failed to compile regex.");
DIE (regexec(&preg, "immunity", 1, &match, 0), "No regex match found");
printf("Match starts at %d and ends at %d.", match.rm_so, match.rm_eo);
return 0;
}
regexec
报告匹配(返回 0),但rm_so
和rm_eo
都等于 0。虽然这是一个匹配(开头是一个空字符串),但手册页暗示总是首先返回最长的匹配,在这种情况下 1 和 3应该退回。当我将正则表达式更改为m+
按预期工作时(1、3,而不是 1、2,这意味着它是贪婪的)。这里发生了什么?
解决方案
推荐阅读
- python - 在 Spyder 中使用 Python 模块而不是命令行
- vba - 像运算符一样访问 vba 只返回第一个结果
- xslt-1.0 - 使用 XSLT 版本 1 以 UTC 获取当前日期时间
- reactjs - 有没有内置 JSX 编译器的浏览器?
- string - 二进制字符串减少
- android - 部分屏幕上的 admob 智能横幅
- oracle - ORA-06502: PL/SQL: 在空游标的情况下出现数字或值错误
- node.js - 将标头发送到客户端(Nodejs、MongoDb、Express)后无法设置标头
- ansible - git remote set-url origin 的 Ansible 等价物
- python - 我的模型的验证准确性卡住了!我如何解决它?[张量流/Keras]