好的,我有一堆C和C++代码,我需要过滤它们,找出函数定义。我不知道函数类型/返回值,也不知道函数定义或函数调用中的参数数量等。
到目前为止,我有:
import re, sys
from os.path import abspath
from os import walk
function = 'msg'
regexp = r"(" + function + ".*[^;]){"
found = False
for root, folders, files in walk('C:\\codepath\\'):
for filename in files:
with open(abspath(root + '/' + filename)) as fh:
data = fh.read()
result = re.findall(regexp, data)
if len(result) > 0:
sys.stdout.write('\n Found function "' + config.function + '" in ' + filename + ':\n\t' + str(result))
sys.stdout.flush()
break然而,这会产生一些不想要的结果。regexp必须是these taulrant,例如以下组合:
找到"msg“的防御,但不是"msg()”调用,在所有的变体中说:
void
shapex_msg (struct shaper *s)
{
msg (M_INFO, "Output Traffic Shaping initialized at %d bytes per second",
s->bytes_per_second);
}或
void shapex_msg (struct shaper *s)
{
msg (M_INFO, "Output Traffic Shaping initialized at %d bytes per second",
s->bytes_per_second);
}或
void shapex_msg (struct shaper *s) {
msg (M_INFO, "Output Traffic Shaping initialized at %d bytes per second",
s->bytes_per_second);
}发布于 2013-04-23 22:47:14
可能类似于下面的正则表达式:
def make_regex(name):
return re.compile(r'\s*%s\s*\([^;)]*\)\s*\{' % re.escape(name))测试您的示例:
>>> text = '''
... void
... shapex_msg (struct shaper *s)
... {
... msg (M_INFO, "Output Traffic Shaping initialized at %d bytes per second",
... s->bytes_per_second);
... }
...
... void shapex_msg (struct shaper *s)
... {
... msg (M_INFO, "Output Traffic Shaping initialized at %d bytes per second",
... s->bytes_per_second);
... }
...
... void shapex_msg (struct shaper *s) {
... msg (M_INFO, "Output Traffic Shaping initialized at %d bytes per second",
... s->bytes_per_second);
... }'''
>>> shapex_msg = make_regex_for_function('shapex_msg')
>>> shapex_msg.findall(text)
['\nshapex_msg (struct shaper *s)\n{', ' shapex_msg (struct shaper *s)\n{', ' shapex_msg (struct shaper *s) {']它仅适用于多行定义:
>>> shapex_msg.findall('''int
shapex_msg (
int a,
int b
)
{'''
['\n \tshapex_msg \t(\nint a,\nint b\n) \n\n\t{']而对于函数调用:
>>> shapex_msg.findall('shapex_msg(1,2,3);')
[]需要注意的是,您的正则表达式无法工作,因为.*很贪婪,因此它没有匹配正确的右括号。
https://stackoverflow.com/questions/16172103
复制相似问题