正则表达式 - 调用另一个的匹配函数

时间:2016-02-11 06:33:29

标签: regex eclipse

所以我正在尝试使用Eclipse搜索查找包含函数调用y的函数(za之间)。

在此代码中:

void y ()
{
    if {    
    }
    if {
    }
}

void z ()
{
    if {
    }
    a(b(c,d,e));
    if {
    }
}

我的正则表达式匹配函数名称之前排除的所有函数yz

\b(y|z) ?\(.+?(\r\n|\r|\n)(?s)\{(\r\n|\r|\n).*?((?<=\r\n|\r|\n)\})

我想要的是一个正则表达式,它只匹配调用函数a的函数。

我尝试过但却失败了:

\b(y|z) ?\(.+?(\r\n|\r|\n)(?s)\{(\r\n|\r|\n).*(\ba\().+?((?<=\r\n|\r|\n)\})

1 个答案:

答案 0 :(得分:0)

我可以通过正则表达式实现这一点,并在python中迭代生成器对象。可能是正则表达式是你需要的唯一东西,但是我在这里发布了整个代码。

import re

mystr = '''
void y ()
{
    if {
    }
    if {
    }
}

void z ()
{
    if {
    }
    a(b(c,d,e));
    if {
    }
}

void y ()
{
    if {
    }
    a("hello");
    if {
    }
}'''

#I've assumed that the return types of both the functions as 'void'. If it is not the case, you've to add few more cases along with 'void' in the regex for other return types

#This is what you need
match = re.finditer(r'void\s*(y|z)\s*\(\s?\)\s*\{[^yz]*a((?!void).|\n|\r|\t)*', mystr)

for x in match:
    print(x.group())

我的输出是:

void z ()
{
    if {
    }
    a(b(c,d,e));
    if {
    }
}

void y ()
{
    if {
    }
    a("hello");
    if {
    }
}
相关问题