- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一个像 051916.000
这样的输入字符串。我想隔离 05
、19
、16
和 000
。我正在尝试以这种方式在 C 语言中使用 regexec
。
regex_t r;
regmatch_t pmatch[4];
char* pattern = "/([0-9]{2})([0-9]{2})([0-9]{2})\\.(.*)";
int status = regcomp(&r, "", REG_EXTENDED|REG_NEWLINE);
status = regexec(&r, t, 4, pmatch, 0);
regfree(&r);
但这似乎行不通。下面是 GDB 输出
(gdb) p pmatch
$1 = {{rm_so = 0, rm_eo = 0}, {rm_so = -1, rm_eo = -1}, {rm_so = -1, rm_eo = -1}, {rm_so = -1, rm_eo = -1}}
我在 Python 中使用过正则表达式。我是 C 中 Regex 的新手。所以我不确定我哪里出错了。正则表达式已验证,并且匹配正确。
最佳答案
这里有一些小错误:
char* pattern = "/([0-9]{2})([0-9]{2})([0-9]{2})\\.(.*)";
你有一个前导斜线。这里的正则表达式没有周围的斜杠;删除它。
status = regcomp(&r, "", REG_EXTENDED|REG_NEWLINE);
在这里,您传递一个空字符串作为模式。你当然想传递“模式”。
regmatch_t pmatch[4];
如果您想捕获所有四个括号内的子表达式,您应该传递一个大小为 5 的数组:pmatch[0]
是整个表达式。
当你修复这些问题时,你的代码就可以工作了:
const char *t = "051916.000";
regex_t r;
regmatch_t pmatch[5];
char* pattern = "([0-9]{2})([0-9]{2})([0-9]{2})\\.(.*)";
int status, i;
status = regcomp(&r, pattern, REG_EXTENDED|REG_NEWLINE);
if (status == 0) status = regexec(&r, t, 5, pmatch, 0);
if (status == 0) {
for (i = 0; i < 5; i++) {
int len = pmatch[i].rm_eo - pmatch[i].rm_so;
const char *str = t + pmatch[i].rm_so;
printf("'%.*s'\n", len, str);
}
}
regfree(&r);
关于c - 使用 regexec 进行分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36807186/
给定一个具有以下结构的二进制文件 unsigned int number1; unsigned int num_rec; acct_info_t acct[num_rec]; acct_info_t
这是我第一次真正尝试使用 C 和正则表达式,所以请耐心等待...我有一个模式数组 (shortener_patterns),其中我将其连同要编译的计数和数组一起传递给 compile_patterns
我遇到了一个问题,其中我的 regexec 代码返回的结果与 Java 代码不同。我使用了相同的正则表达式,但在 c++/c 中返回不同。 这是我的代码片段: const char *pattern
我正在尝试使用以下正则表达式匹配“sdb-iof-pool 1008.56M 884K”等字符串:^(.*)([\s]+)([-+]?[0-9 ]*\.?[0-9]+)([K|M|G|T|P]{1})
我正在实现一个“银行”软件,在创建新用户时必须采用可接受的用户名、密码和余额。 BANK: create-user bob 1234 11111 这是正确输入的示例 BANK: create-user
我需要检查月份是否为 01-12 的形式。我制作了一个正则表达式并输入以检查它是否与正则表达式匹配。代码- #include #include using namespace std; #inclu
我试图在一个大的内存映射文件中找到一个正则表达式通过使用 regexec() 函数。我发现程序崩溃时文件大小是页面大小的倍数。 是否有一个regexec() 函数,它具有字符串的长度作为附加参数? 或
我试图在一个大的内存映射文件中找到一个正则表达式通过使用 regexec() 函数。我发现程序崩溃时文件大小是页面大小的倍数。 是否有一个regexec() 函数,它具有字符串的长度作为附加参数? 或
在以下过于简化的代码示例中: reg = "^[0-9]{1,10}$"; str = "123abc"; regcomp(&re, reg, REG_EXTENDED); regexec(&re,
我有一个像 051916.000 这样的输入字符串。我想隔离 05、19、16 和 000。我正在尝试以这种方式在 C 语言中使用 regexec。 regex_t r; regmatch_t pma
我有一个模式 "^\+?\d{3,20}$" 对“123455”、“+123445”进行测试,预期匹配。 测试“123+213”、“abc”,预计不会匹配。 此模式适用于 pcre_exec(),但不
我正在尝试创建一个正则表达式来评估以下划线开头的数字序列,如下所示:_201312我对数字部分有疑问;我总是收到 REG_NOMATCH 消息。这是代码: #include int result_e
谁能告诉我使用哪个库来利用 regexec()、regfree() 和 regcomp() 函数?我知道它们是在 regex.h 头文件中定义的,但即使在包含头文件之后,当我尝试在 HP-UX 中编译
正则表达式模式需要匹配以下内容: abc_xyz_0 abc_1025_01.29.00_xyz_0 abc_0302_42.01.00_xyz_0 (abc 和 xyz 之间的数字无关紧要) 所以我
标准的C和C++都不支持正则表达式,但有一些函数库可以辅助C/C++程序员完成这一功能,其中最著名的当数Philip Hazel的Perl-Compatible Regular Expression
有没有办法拿到libc6的正则表达式函数 regcomp和 regexec使用多字节字符正常工作? 例如,如果我的模式是 utf8 字符 猫机+猫 , 在 utf8 编码字符串上找到匹配项 猫机机机猫
这个问题已经有答案了: Why is the following regex not working in C using regcomp (2 个回答) 已关闭 3 年前。 这是我的代码: #inc
我有以下脚本,它解析进程内存以查找字符串匹配,一切正常,但转储编辑器(在本例中为纳米)的进程与 1193 可能的匹配(如果我转储内存,然后对其执行egrep),但我的代码仅输出 3 匹配项。有什么想法
我正在尝试检查字符串是否为有效数字。有效数字类似于 123 或 123.456。我尝试使用 [0-9]+(\\.[0-9]+)? 进行匹配,但这不起作用。 这是我目前所拥有的。 #include #
我有这个代码: char *buffer = calloc(400, sizeof(char)); // buffer gets assigned a string through fgets if
我是一名优秀的程序员,十分优秀!