Rex Kim
Rex Kim

Reputation: 46

Finding function calls using regex

I'm trying to detect functions between call-chains.

For example, I can use

re.search("([\w_]+)\((|[\W\d\w\,]+)\)", line)

to find

print(len("AA"))

but it is reasonably not compatible with code like:

print(i + len("AA") + j + len("BBB"))

Help me.

Upvotes: 1

Views: 163

Answers (2)

Bohemian
Bohemian

Reputation: 424953

Use this regex:

(\w+)\(((?:[^()]*\([^()]*\))*[^()]*)\)

This captures the name of the function in group 1, and the contents the brackets (the parameters) in group 2.

See a live demo of this regex working with your examples.


BTW, Your regex could use some attention:

  • [\w_]+ is equivalent to just \w+ because \w includes underscore
  • [\W\d\w\,] is equivalent to just ., because the combination \W\w (everything not a word char and every word char) includes everything

Upvotes: 0

chthonicdaemon
chthonicdaemon

Reputation: 19750

Your needs may be better served by the ast module:

import ast

a = 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)])

Upvotes: 1

Related Questions