Well, you didn’t choose a specific language so I’ll be doing it with PHP because I have greater familiarity but the ER itself I believe is functional in other languages as long as they support lookaround assertions and, if necessary, receive appropriate language-specific adjustments:
/function ((?!edit|busca|edit)\w+)\((.*?)\)\{[\x00-\xFF]+\\1\(\\2\)[\x00-\xFF]+\}/
Is married a word (\w+
) other than one of the prohibited ((?!(palavra|palavra|palavra))
).
Then the parentheses are married with anything inside for a possible list of arguments. You can remove if you don’t need them.
Then the bounding keys of a code block are married and within them any character ([\x00-\xFF]+
), followed by our previously married function (\\1
), the parentheses and their content (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 );
Just the first house something, because:
- In the second the function is not recursively called
- On the third we have a forbidden name
- In the room 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
What you are trying to do is not only done with traditional regular expressions as they are defined. However javascript uses extended regular expressions with Lookahead, lookbehind and backreferences and can pick up some things that are not actually regular languages. I’ll take a look at this to see if I can help you, the path seems to be backreferences.
– Victor Stafusa
If I understand correctly you have this question that can help you http://answall.com/questions/26144/2-express%C3%B5es-regular-in-1
– jsantos1991
jsantos1991. Thank you for the reference. with it I found the solution.
– Guilherme Lautert