Sahil
Sahil

Reputation: 1983

Regex expression to extract everything inside brackets

I need to extract content inside brackets () from the following string in C++;

#82=IFCCLASSIFICATIONREFERENCE($,'E05.11.a','Rectangular',#28);

I tried following regex but it gives an output with brackets intact.

std::regex e2 ("\\((.*?)\\)");
if (std::regex_search(sLine,m,e2)){
}

Output should be:

 $,'E05.11.a','Rectangular',#28

Upvotes: 1

Views: 1964

Answers (3)

Nicola Musatti
Nicola Musatti

Reputation: 18218

The result you are looking for should be in the first matching subexpression, i.e. comprised in the [[1].first, m[1].second) interval.

This is because your regex matches also the enclosing parentheses, but you specified a grouping subexpression, i.e. (.*?). Here is a starting point to some documentation

Upvotes: 2

Srb1313711
Srb1313711

Reputation: 2047

(?<=\().*(?=\))

Try this i only tested in one tester but it worked. It basically looks for any character after a ( and before a ) but not including them.

Upvotes: 1

Song Gao
Song Gao

Reputation: 666

Use lookaheads: "(?<=\\()[^)]*?(?=\\))". Watch out, as this won't work for nested parentheses.

You can also use backreferences.

Upvotes: 1

Related Questions