Take the 2-minute tour ×

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 the function calls that I'm looking for.

  • The function calls will have a single, specific name.
  • The function calls may be chained, but will only have one chained call that will always have the same name.
  • The first function will always take a single string argument.
  • The chained function, however, might take arbitrary arguments (this is the one that worries me).

Here are example usages of the functions I want to look for within a file:

# Simple function call.

# The chained function call, in the simplest format (no args).

# The chained function call with simple arguments.
f("_key").g("hello", 1337)

# The chained function call with possible, more complex arguments
f("_key").g(obj.blah(), {"dog":"cat"})

# And then the possibility for long function calls to extend over one line

And the usual disclaimer: I did a search for this, and the questions were close to mine, but I'm wondering if my needs are constrained enough to get around the "regular vs. irregular" language problem. This is what I get for not being a computer science major and being afraid of regular expressions.

share|improve this question
Are you sure that regular expressions are the best tool for this? Python has several builtin libraries for code introspection. See docs.python.org/library/language.html –  DaveP Dec 13 '11 at 6:09
@DaveP I'm not. I habitually think of Regular Expressions because, well, it's sort of like SEs who turn to PERL. I've learned a lot teaching myself programming (started with C), but there is some classic stuff I just missed not getting a CS degree, like learning about grammars. I really like Raymond's answer below, so I'm going to try that out first, as I have a feeling it's more reliable and less error prone, plus I'll learn a lot. I'm also going to test Sylverdrag's answer, as I think his might be a very good short term solution (as long as the tests I've put together pass). –  jeremyosborne Dec 13 '11 at 16:01
add comment

2 Answers

up vote 2 down vote accepted

This should do what you want:

share|improve this answer
Thanks Sylverdrag. I believe this will be a good short term solution. Let me test it out and make sure it doesn't vomit when parsing my python files. [EDIT: Rather, let me make sure my python files don't break your regex ;) ] –  jeremyosborne Dec 13 '11 at 16:03
Thanks Sylverdrag, this is what I'm looking for. A few tweaks to this will serve me well right now. –  jeremyosborne Dec 14 '11 at 20:18
add comment

FWIW, here is an excerpt from Grammar/Grammar:



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

                         |'**' 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:

share|improve this answer
Thanks Raymond. I believe you are answering what my real question is, which is how to do this reliably and have it be (essentially) bullet proof. I'll try this out later today when I have a spare moment. –  jeremyosborne Dec 13 '11 at 16:02
add comment

Your Answer


