使用Python正则表达式在文件中查找函数调用的正则表达式? [英] Regular expression to find function calls in a file with Python regular expression?

查看:77
本文介绍了使用Python正则表达式在文件中查找函数调用的正则表达式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个正则表达式,我将在 Python re 模块中使用它,它将在 python 文件中查找 python 函数调用,但是我正在查找的函数调用周围会有一些警告.

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.

  • 函数调用将有一个特定的名称.
  • 函数调用可能是链式的,但只有一个链式调用且始终具有相同的名称.
  • 第一个函数将始终采用单个字符串参数.
  • 然而,链式函数可能接受任意参数(这是我担心的一个).

以下是我想在文件中查找的函数的示例用法:

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

# Simple function call.
f("_key")

# The chained function call, in the simplest format (no args).
f("_key").g()

# 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
f("_key").g(
            "dogs",
            "cats",
            {"living":"together"})

和通常的免责声明:我搜索了这个,问题与我的很接近,但我想知道我的需求是否受到足够的限制来解决常规与不规则"语言问题.这就是我不是计算机科学专业并且害怕正则表达式的结果.

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.

推荐答案

这应该可以满足您的需求:

This should do what you want:

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

这篇关于使用Python正则表达式在文件中查找函数调用的正则表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆