python - Finding function calls using regex -
i'm trying detect functions between call-chains.
for example, can use
re.search("([\w_]+)\((|[\w\d\w\,]+)\)", line)
to find
print(len("aa"))
but reasonably not compatible code like:
print(i + len("aa") + j + len("bbb"))
help me.
your needs may better served ast
module:
import ast = ast.parse('print(i + len("aa") + j + len("bbb"))') print ast.dump(a) >>> module(body=[print(dest=none, values=[binop(left=binop(left=binop(left=name(id='i', ctx=load()), op=add(), right=call(func=name(id='len', ctx=load()), args=[str(s='aa')], keywords=[], starargs=none, kwargs=none)), op=add(), right=name(id='j', ctx=load())), op=add(), right=call(func=name(id='len', ctx=load()), args=[str(s='bbb')], keywords=[], starargs=none, kwargs=none))], nl=true)])
Comments
Post a Comment