views:

245

answers:

3

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.

+6  A: 

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:

/\<if\>[^{]*;/

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.

David Schmitt
+3  A: 

If you want a 100% working solution then a regex will not fit the bill. It's way too easy to trip up a regex with real code. Take for instance the following regex

"^\s*if\W[^{]*\n\s*[^{]"

This will match a good majority of "if" statements that are not surrounded by braces. However it can easily be broken. Take the following samples. The regex will incorrectly flags these as if statements with braces.

Example 1

if ( SomeFunction(() => { return 42; }) )

Example 2

/* Big comment
   if ( true ) {
*/

The list goes on and on.

Bottom line, if you want perfection, a regex will not work. If you are satisfied with a less than perfect solution then the above regex should do the trick.

JaredPar
A: 

You can Google for Finite-State Machine to find out why it is not possible to write a pure regular expression you are asking for.

On the other hand some reg-ex interpreters, like in the Perl language have the possibility to reference previously matched expression, which make it possible to theoretically implement entire C# grammar. Do not follow this idea though ;) David Schmitt idea is nice.

agsamek