Java Regex - 解析函数名和参数名 - 抛出PatternSyntaxException:Unclosed Character

时间:2017-05-02 10:35:51

标签: java regex regex-lookarounds

这是我用来提取函数名称的正则表达式

正则表达式

^(\w+(\s+)?){2,}\([^!@#$+%^]*\)

代码

final static String functionNameRegex = "^(\\w+(\\s+)?){2,}\\([^!@#$+%^]*\\)";
final static String functionString = "public void render(int screenNo, String infoText){}";


final Pattern fnPattern = Pattern.compile(functionNameRegex, Pattern.CASE_INSENSITIVE | Pattern.COMMENTS);
final Matcher fnMatcher = fnPattern.matcher(functionString);

while (fnMatcher.find()) {
       System.out.println("Full match: " + fnMatcher.group(0));
       for (int i = 1; i <= fnMatcher.groupCount(); i++) {
             System.out.println("Group " + i + ": " + fnMatcher.group(i));
       }
}

但是,它会抛出PatternSyntaxException。不知道是什么导致了这个问题,因为我能够以其他语言获得所需的输出。

痕迹:

Exception in thread "main" java.util.regex.PatternSyntaxException: Unclosed character class near index 30
^(\w+(\s+)?){2,}\([^!@#$+%^]*\)
                              ^
    at java.util.regex.Pattern.error(Pattern.java:1955)
    at java.util.regex.Pattern.clazz(Pattern.java:2548)
    at java.util.regex.Pattern.sequence(Pattern.java:2063)
    at java.util.regex.Pattern.expr(Pattern.java:1996)
    at java.util.regex.Pattern.compile(Pattern.java:1696)
    at java.util.regex.Pattern.<init>(Pattern.java:1351)
    at java.util.regex.Pattern.compile(Pattern.java:1054)
    at TestRegex.main(TestRegex.java:28)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

正则表达式的新功能,帮助我重新排列此正则表达式以识别和分组参数名称。

1 个答案:

答案 0 :(得分:4)

您使用Pattern.COMMENTS编译正则表达式,并且模式中包含#符号。你需要逃避它,否则,它表示评论的开始。

使用

final static String functionNameRegex = "^(\\w+(\\s+)?){2,}\\([^!@\\#$+%^]*\\)";

请参阅Java demo

另外,如果您计划匹配2个或更多空格分隔的单词(它会更快地运行),请考虑将(\\w+(\\s+)?){2,}更改为(\\w+(?:\\s+\\w+)+)。(第1组仍将保留括号前的值)。< / p>