如何匹配字串直到兩個換行符?

如何匹配字串直到兩個換行符?

我知道這裡已經有很多類似的問答,但我無法將它們放在一起。

我想要的是將特定函數呼叫與正規表示式相匹配,例如:

Lib.myfunction( arg0, arg1,
                arg2, arg3 )

我正在尋找Lib.myFunction專門的,它不一定是完全通用的。每個這樣的函數呼叫後面都有額外的空行。

如果它可以用換行符終止,然後是可選的空格,然後換行符,那就沒什麼額外的了,因為編輯器傾向於添加空格以與前一行的文字對齊。

您知道正規表示式應該是什麼樣子嗎?

答案1

Lib\.myfunction\s*\(\s*\S+(?:,\s*\S+)*\s*\)

解釋:

Lib\.myfunction     # literally
\s*                 # 0 or more spaces
\(                  # opening parenthesis
    \s*             # 0 or more spaces
    \S+             # 1 or more NON spaces
    (?:             # start non capturing group
        ,           # a comma
        \s*         # 0 or more spaces
        \S+         # 1 or more NON spaces
    )*              # end group, may appear 0 or more times
    \s*             # 0 or more spaces
\)                  # closing parenthesis

示範

相關內容