Kamath
Kamath

Reputation: 4664

Regex to check parentheses for C code

I am quite new to regex, and I use them only for searching. So I play around with egrep, but with egrep I see some limitations.

For linux are there any good tool for regex? I know there is sed but its usually used in scenarios to search and replace. Can I used sed only to search through my files ? If yes how ?

Coming to my specific question, I need to search for instances in C source code where additional parentheses are used for if statements.

if (( a == b)) should match and if (a == b) should not match.
if(a && ((b||c))) should match and if(a && (b||c)) should not match.

I want it to be simple, I will add additional cases later like >= != etc.

Upvotes: 1

Views: 203

Answers (1)

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272487

This is not possible with regexes (at least not on their own), as they cannot keep track of arbitrary levels of nesting of parentheses.

Upvotes: 5

Related Questions