How to search complex source code using regular expressions

Today I needed to search the source code that ware calling a function with a complex array of parameters

URL::Make( ‘site/’, array( ‘action’=>’view’,’id’=> [… STH…] ,’idTwo’=> [… STH…] ) );

Considering this requirements:

– there are lots of similar function (to exclude from the search) with an argument less or an argument more

– there may are additional spaces in the line

– some other delopers have probably used double quotes instead of single quotes for array keys

– the value of the keys may be a PHP code

– every function call is in one line

An elegant solution is to search using regular expression. Netbeans (my favorite editor) supports POSIX extended regular expression and is very quickly to search a complex epression in a huge amount of source code.

Solution 1

#using char length and patter “.{0,MAX}” (that is: every sequence, MAX maximum chars). Easy !


Solution 2

#using regexpr for separators. more complex. more accurate in some cases but fails in some cases (eg: comments inside)

URL::Make.*([ ]*[‘”]site/[‘”][ ]*,[ ]*array[ ]*([ ]*[‘”]action[‘”][ ]*=>[ ]*[‘”]view[‘”][ ]*,[ ]*[‘”]id[‘”][ ]*=>.{0,100}[‘”]idTwo[‘”][ ]*=>.{0,100}[ ]*)[ ]*)