I want to find all if statements and for statements which are not followed by curly brackets ‘{‘. When you write a single line in an if statement you do not mostly enclose it in curly brackets, so I want to find all those if and for statements.
Please help!
Like I want to capture this statement
if (childNode.Name == 'B') return TokenType.Bold;
but not these kinds
if (childNode.Name == 'B') { return TokenType.Bold; }
I want to do it with regex.
Since the underlying mathematics disallow a perfect match, you could go for a good heuristic like ‘find all ‘
if‘ followed by a semicolon without an intervening open brace:where
\<and\>are begin-of-word and end-of-word as applicable to your regex dialect. Also take care to ignore all newlines in the input, some regex processors need to be told to do that.You might want to look at StyleCop too. This is a tool which runs a big set of various checks on your source code. This check is already there.