为什么以下正则表达式在使用 regcomp 的 C 中不起作用 [英] Why is the following regex not working in C using regcomp

查看:75
本文介绍了为什么以下正则表达式在使用 regcomp 的 C 中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下正则表达式来匹配字符串中的最后一对大括号,

I have the following regex to match the last pair of braces in a string,

.+(?={)(.+)(?=})

示例字符串是,

abc{abc=bcd}{gef=hij}

我想要捕获组内最后一个大括号 (gef=hij) 中的内容.这适用于网络上可用的正则表达式测试器

I want the contents within the last braces (gef=hij) inside the captured group. This works in a regex tester available in the web

http://regexpal.com/

当我使用 regcomp 编译相同的正则表达式时,它没有.有什么想法吗?

When I use regcomp to compile the same regex, it doesnt. Any ideas?

int reti = regcomp(&regex, ".+(?={)(.+)(?=})", REG_EXTENDED);
if (reti) {
    fprintf(stderr, "Could not compile regex\n");
    exit(1);
}

推荐答案

无论如何,regcomp 使用 POSIX BRE 或 ERE,它们不支持前瞻或后视.

Anyway, regcomp uses POSIX BRE or ERE, which doesn't support look-ahead or look-behind.

.+{(.+)}

从组索引 1 中获取您想要的字符串.

Grab the string you want from group index 1.

演示

这篇关于为什么以下正则表达式在使用 regcomp 的 C 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆