有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

regex Java正则表达式只匹配签名中的方法名称

我有一个表示方法签名的字符串列表。例如:

public String someMethod(String parameter)

public static void someAnotherMethod(double doubleParam, List<String> stringList)

通过使用以下正则表达式(linkhttps://regex101.com/r/TwRRbp/1),我可以获得方法名和参数:

\w+\(.*\)

最后,我得到以下信息:

someMethod(String parameter)

someAnotherMethod(double doubleParam, List<String> stringList)

但我只需要方法名。我想我需要把重点放在开头的括号上


共 (1) 个答案

  1. # 1 楼答案

    使用

    [a-zA-Z0-9_]+(?=\()
    

    proof

    解释

                                            
      [a-zA-Z0-9_]+            any character of: 'a' to 'z', 'A' to 'Z',
                               '0' to '9', '_' (1 or more times (matching
                               the most amount possible))
                                            
      (?=                      look ahead to see if there is:
                                            
        \(                       '('
                                            
      )                        end of look-ahead