Java 匹配(基本)函数声明

Java 匹配(基本)函数声明,java,regex,Java,Regex,我想从源代码文件中分别检索所有函数定义。最后,我只想检索所有函数名。源文件的格式如下: #include bla first_function_name() { } second_function_name(first_parameter, second_parameter) { i = 0; } 注意,没有访问修饰符和返回类型,这是解析Java编程语言的而不是 我想通过实现解决方案。到目前为止,我成功地匹配了函数定义,但是我遇到了一个问题,正则表达式不仅匹配单个函数,而且还匹配后

我想从源代码文件中分别检索所有函数定义。最后,我只想检索所有函数名。源文件的格式如下:

#include bla

first_function_name()
{
}

second_function_name(first_parameter, second_parameter)
{
    i = 0;
}
注意,没有访问修饰符和返回类型,这是解析Java编程语言的而不是

我想通过实现解决方案。到目前为止,我成功地匹配了函数定义,但是我遇到了一个问题,正则表达式不仅匹配单个函数,而且还匹配后面的函数。基本上,它不会在右大括号结束。我尝试使用
$
符号,但它也没有结束正则表达式

我当前使用的正则表达式如下所示:

private static final String FUNCTION_NAME_MATCHER = "[a-zA-Z]\\w*";
private static final String FUNCTION_MATCHER = "(?s)" + FUNCTION_NAME_MATCHER + "[(].*[)].*[\\{]([^\\}]*)?[\\}]";
public List<String> getMatches()
{
    List<String> matchedResults = new ArrayList<>();
    Matcher matcher = Pattern.compile(FUNCTION_MATCHER).matcher(sourceFile);

    while (matcher.find())
    {
        String functionDefinition = matcher.group();
        String functionName = functionDefinition.split(FUNCTION_NAME_MATCHER)[0];
        matchedResults.add(functionName);
    }

    return matchedResults;
}
如何阻止它与以下函数匹配?对于上述示例函数,它应该匹配两次,但只匹配一次(两个函数定义同时匹配)

获取匹配函数定义列表的方法如下所示:

private static final String FUNCTION_NAME_MATCHER = "[a-zA-Z]\\w*";
private static final String FUNCTION_MATCHER = "(?s)" + FUNCTION_NAME_MATCHER + "[(].*[)].*[\\{]([^\\}]*)?[\\}]";
public List<String> getMatches()
{
    List<String> matchedResults = new ArrayList<>();
    Matcher matcher = Pattern.compile(FUNCTION_MATCHER).matcher(sourceFile);

    while (matcher.find())
    {
        String functionDefinition = matcher.group();
        String functionName = functionDefinition.split(FUNCTION_NAME_MATCHER)[0];
        matchedResults.add(functionName);
    }

    return matchedResults;
}
public List getMatches()
{
List matchedResults=new ArrayList();
Matcher Matcher=Pattern.compile(函数\ Matcher.Matcher)(源文件);
while(matcher.find())
{
String functionDefinition=matcher.group();
字符串functionName=functionDefinition.split(函数名称匹配器)[0];
matchedResults.add(functionName);
}
返回匹配的结果;
}
试试这个

private static final String FUNCTION_NAME_MATCHER = "([a-zA-Z]\\w*)";
private static final String FUNCTION_MATCHER = "(?s)" + FUNCTION_NAME_MATCHER + "\\([^)]*\\)\\s*\\{[^}]*\\}";

public static List<String> getMatches()
{
    List<String> matchedResults = new ArrayList<>();
    Matcher matcher = Pattern.compile(FUNCTION_MATCHER).matcher(sourceFile);

    while (matcher.find())
    {
        matchedResults.add(matcher.group(1));
    }

    return matchedResults;
}
private static final String FUNCTION_NAME_MATCHER=“([a-zA-Z]\\w*)”;
私有静态最终字符串函数\u MATCHER=“(?s)”+函数\u NAME\u MATCHER+“\\([^]*\\)\\s*\{[^}]*\}”;
公共静态列表getMatches()
{
List matchedResults=new ArrayList();
Matcher Matcher=Pattern.compile(函数\ Matcher.Matcher)(源文件);
while(matcher.find())
{
matchedResults.add(matcher.group(1));
}
返回匹配的结果;
}
试试这个

private static final String FUNCTION_NAME_MATCHER = "([a-zA-Z]\\w*)";
private static final String FUNCTION_MATCHER = "(?s)" + FUNCTION_NAME_MATCHER + "\\([^)]*\\)\\s*\\{[^}]*\\}";

public static List<String> getMatches()
{
    List<String> matchedResults = new ArrayList<>();
    Matcher matcher = Pattern.compile(FUNCTION_MATCHER).matcher(sourceFile);

    while (matcher.find())
    {
        matchedResults.add(matcher.group(1));
    }

    return matchedResults;
}
private static final String FUNCTION_NAME_MATCHER=“([a-zA-Z]\\w*)”;
私有静态最终字符串函数\u MATCHER=“(?s)”+函数\u NAME\u MATCHER+“\\([^]*\\)\\s*\{[^}]*\}”;
公共静态列表getMatches()
{
List matchedResults=new ArrayList();
Matcher Matcher=Pattern.compile(函数\ Matcher.Matcher)(源文件);
while(matcher.find())
{
matchedResults.add(matcher.group(1));
}
返回匹配的结果;
}

*
是贪婪的,它将选择它能找到的所有可能的匹配字符。现在
[(].*[)]
部分正在消耗从第一个
在第一个函数中一直到最后一个
在第二个函数中开始的所有内容。你想让它不情愿,它只会在需要的时候消耗一个角色。通过将所有
*
更改为
*?

另外,您可能希望只匹配函数声明和正文之间的空格,因此应该将
[)].[\\{]
替换为
[)]\\s*[\{]


如果您将
函数\u NAME\u MATCHER
和带有
的参数括起来,它将被捕获到一个捕获组中,以便您可以提取它。

*
是贪婪的,它将选择它能找到的所有可能的匹配字符。现在
[(].[])]
部件从第一个
在第一个函数中一直到最后一个
在第二个函数中消耗所有内容。您想让它不情愿,因为它只会在需要时消耗一个字符。通过将所有
*
更改为
*?

另外,您可能希望只匹配函数声明和正文之间的空格,因此应该将
[)].[\\{]
替换为
[)]\\s*[\{]


如果将
函数\u NAME\u MATCHER
和带有
的参数括起来,它将被捕获到一个捕获组中,以便您可以提取它。

首先,您希望匹配整个函数,以避免匹配函数调用和重复:

[^\s]*\(([^}]*)\)\{([^}]*)}
然后,您要将其拆分以获得名称:

String matchedName = matchedFunction.split("(")[0]

好了!一切都完成了!首先,您需要匹配整个函数,以避免匹配函数调用和重复:

[^\s]*\(([^}]*)\)\{([^}]*)}
然后,您要将其拆分以获得名称:

String matchedName = matchedFunction.split("(")[0]

好了!一切都搞定了!

你有这种语言的语法吗?然后你可以使用类似的东西。@AndyTurner:不是真的,但它是一种基本的脚本语言,没有什么特别之处。它是基于CDo的。你有这种语言的语法吗?然后你可以使用类似的东西。@AndyTurner:不是真的,但它是一种基本的脚本语言ting语言,没什么特别的。它是基于C语言的