[英]Java Regular Expression to match only method name in signature
我有一個代表方法簽名的字符串列表。 例如:
public String someMethod(String parameter)
public static void someAnotherMethod(double doubleParam, List<String> stringList)
通過使用以下正則表達式(鏈接https://regex101.com/r/TwRRbp/1 ),我可以獲得方法名稱和 arguments:
\w+\(.*\)
最后,我得到以下信息:
someMethod(String parameter)
someAnotherMethod(double doubleParam, List<String> stringList)
但我只需要方法名稱。 我想我需要關注左括號。
你說得很對。 使用正向 Lookahead,您可以匹配后跟左括號的任何單詞字符。 如果你想確定,你可以搜索單詞,后跟括號和大括號:
(\w+)(?=\(.*\)\s*\{)
利用
[a-zA-Z0-9_]+(?=\()
見證明。
解釋
--------------------------------------------------------------------------------
[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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.