不清楚 submatch 在 vi​​m 中的作用

不清楚 submatch 在 vi​​m 中的作用

我不清楚 vim 中的 submatch({nr}) 表達式的作用。手冊中有以下內容:

submatch({nr})                      *submatch()*
    Only for an expression in a |:substitute| command.  Returns
    the {nr}'th submatch of the matched text.  When {nr} is 0
    the whole matched text is returned.
    Example:
        :s/\d\+/\=submatch(0) + 1/
    This finds the first number in the line and adds one to it.
    A line break is included as a newline character.

我明白 submatch(0) 的作用,但是「第 {nr} 個子匹配」是什麼意思?

答案1

第 {nr} 個表示第 {number} 個。在這種情況下,當在替換中不使用表達式時,它將與 \0、\1、...\9 相同。看:

:help /\1

子匹配在模式中用 \(...\) 括起來。看:

:help /\(

相關內容