I’ve gotten bitten more than once by a bug where you accidentally assign a variable inside a conditional statement, like if.
I’m looking to grep my sources for such occurances. I created an expression that works in PHP for the simple case, but was wondering if anyone could optimize it or handle the more interesting cases.
Samples:
if ($var = 3) //MATCH
if($var = 3) //MATCH
if($var=3) //MATCH
if ( $var = 3) //MATCH
if ($var == $var2)
if ($var = $var3) //MATCH
if ( $var === 7)
if( $var == 8 || $var = 9) //MATCH
if (($var == 7 ) && ($var ==10))
The simple cases are handled well by if\s*\([\$a-zA-Z\d\s]*=[\$a-zA-Z\d\s]*\) but it would be nice to come up with something that works for the extended versions at the bottom of the sample.
Any ideas on better expressions?
First, let’s assume you don’t have cases like this (C example):
because understanding these cases needs an actual parser.
Now, simply put, you want to match
=but not==\+. Therefore:What it says:
if: match the first if.[^=]: any character but===\+:==and===\([^=]\|==\+\)*: anything that is not=or is==or===. This includes all the whitespace, the beginning(, ending)etc. This comes both before and after the isolated=.[^=]=[^=]: isolated=.