Well, you did not choose a specific language so I'll be doing with PHP for more familiarity but the ER itself I believe to be functional in other languages as long as they support lookaround assertions and if necessary, receive the appropriate language-specific adjustments:
/function ((?!edit|busca|edit)\w+)\((.*?)\)\{[\x00-\xFF]+\1\(\2\)[\x00-\xFF]+\}/
A word ( \w+
) that is not one of the forbidden ( (?!(palavra|palavra|palavra))
) is married.
Then the parentheses are matched with anything inside for a possible list of arguments. You can remove it if you do not need them.
Then the bounding keys of a block of code are married, and within them any character ( [\x00-\xFF]+
), followed by our previously married function ( \1
), the parentheses and their contents (also removable) and anything new, so the function can appear anywhere in the code block.
The tests:
$str1 = 'function rl($a){
rl($a)
}';
$str2 = 'function rl($a){
//code
}';
$str3 = 'function index($a){
anotherfunction($a)
}';
$str4 = 'function edit($a){
edit($a)
}';
$str5 = 'function rl(){
edit();
}';
preg_match( '/function ((?!edit|busca|edit)\w+)\((.*?)\)\{[\x00-\xFF]+\1\(\2\)[\x00-\xFF]+\}/', $str1, $m1 );
preg_match( '/function ((?!edit|busca|edit)\w+)\((.*?)\)\{[\x00-\xFF]+\1\(\2\)[\x00-\xFF]+\}/', $str2, $m2 );
preg_match( '/function ((?!edit|busca|edit)\w+)\((.*?)\)\{[\x00-\xFF]+\1\(\2\)[\x00-\xFF]+\}/', $str3, $m3 );
preg_match( '/function ((?!edit|busca|edit)\w+)\((.*?)\)\{[\x00-\xFF]+\1\(\2\)[\x00-\xFF]+\}/', $str4, $m4 );
preg_match( '/function ((?!edit|busca|edit)\w+)\((.*?)\)\{[\x00-\xFF]+\1\(\2\)[\x00-\xFF]+\}/', $str5, $m5 );
var_dump( $m1, $m2, $m3, $m4, $m5 );
Only the first house something, therefore:
- In the second the function is not called recursively
- In the third we have a forbidden name
- In the fourth we have a forbidden name and a recursion of a forbidden name
- In the fifth we have a valid name, but without having the function called recursively