I have a code.py:
def funA():
print('A')
funA()
def funB():
print('B')
def funC():
print('C')
funB()
funC()
I want to find all functions call themself:
funA
funC
How to write the regex?
Constraint:
- All function call is normal:
funname(arg1, arg2, ...) - No obfuscated ways(such as
lambda,exec) - No indirect recursion
Yes I do believe that it would be impossible for a regex to match the cases pointed out by wim where the self call is obfuscated. However, here is a regex that will do a semi decent job for straightforward self calls (in the form
funcname(...)). This regex correctly matches all the test cases spelled out in the original question:It matches starting with the function definition line and captures the whitespace indentation before the
'def'in group$1and the function name in group$2. It then matches lines within the function block that do not contain the function name which each have more leading whitespace than the function definition. It skips over empty lines and lines containing only comments. It declares a match once it finds a line in the function block which has the function name followed by a left parentheses, indicating a call to itself. Otherwise, it declares a non-match then continues looking for the next possible match.Note that this solution is unreliable and will result in false positives if the name of the function occurs inside a string or inside a comment following other code on a line. It also does not handle functions with multi-line raw strings. However, it will correctly catch quite a few!