Why is the following regex not working in C using regcomp

我的未来我决定 提交于 2020-01-30 11:18:32

问题


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

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

The example string is,

abc{abc=bcd}{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/

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);
}

回答1:


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

.+{(.+)}

Grab the string you want from group index 1.

DEMO




回答2:


Uses an anchor to specify the pattern should match when at the end of a line.

(?<=[{]).*(?=[}]$)



来源:https://stackoverflow.com/questions/27188328/why-is-the-following-regex-not-working-in-c-using-regcomp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!