0

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);
}
nhahtdh
  • 55,989
  • 15
  • 126
  • 162
Karthik H
  • 123
  • 1
  • 11

2 Answers2

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

Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
-1

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

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

SierraOscar
  • 17,507
  • 6
  • 40
  • 68