Skip to content Skip to sidebar Skip to footer

Regular Expression To Find Function Calls In A File With Python Regular Expression?

I would like a regular expression, of which I will use with the Python re module, that will look for python function calls within a python file, but there will be caveats around th

Solution 1:

This should do what you want:

[a-zA-Z]+\([^\)]*\)(\.[^\)]*\))?

Solution 2:

FWIW, here is an excerpt from Grammar/Grammar:

decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE

trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME

power: atom trailer* ['**'factor]
atom: ('(' [yield_expr|testlist_comp] ')' |
       '[' [listmaker] ']' |
       '{' [dictorsetmaker] '}' |
       '`' testlist1 '`' |
       NAME | NUMBER | STRING+)

arglist: (argument ',')* (argument [',']
                         |'*'test (',' argument)* [',''**'test] 
                         |'**'test)

These are the cases that need to be handled by a regex to capture all function calls without any false positives.

Instead of regexes, perhaps it would be better to leverage one of the toolsets that come with the Python standard library:

Post a Comment for "Regular Expression To Find Function Calls In A File With Python Regular Expression?"