Match parenthesised block using regular expressions in vim

后端 未结 4 621
终归单人心
终归单人心 2020-12-03 22:14

I\'m trying to match the contents that belong between a certain ( and its matching ) as found by vim when using the motion %.

相关标签:
4条回答
  • 2020-12-03 22:41

    As Jens told, this cannot be done with Vim regular expressions.

    However if you want to do some special processing you can possibly do the following:

    • Find opening parentheses that interest you and then run a macro that will insert some character (let's say ^@) after the opening parenthese and before the closing parenthese using the % binding.
    • Then modify the regex accordingly.
    0 讨论(0)
  • 2020-12-03 22:45

    Most regex dialects can't do this. It is possible with the .NET implementation (see here), but very, very ugly and should not be used in my opinion.

    0 讨论(0)
  • 2020-12-03 22:48

    You could do this quite easily with a macro. You would search for the keyword with a regexp and then search for the start of the text, then the end of it, then you can undertake what ever action that you want.

    E.g. to yank the required text:

    qa/somekeyword\s*
    /(
    lvh%hyq
    

    Now whenever you use the macro with @a you would yank the next bit of text that you are interested in.

    0 讨论(0)
  • 2020-12-03 22:57

    This is not possible with vim regular expressions (as language that allows such nested constructs is not regular), but is possible with 'regular' expressions provided by perl (as well as by other languages I do not know enough to be sure) and perl can be used from inside vim. I don't like vim-perl bindings (because it is very limited), but if you know all cases that should work, then you could use recursion feature of perl regular expressions (requires newer perl, I have 5.12*):

    perl VIM::Msg($+{"outer"}) if $curbuf->Get(3) =~ /someKeyword\((?'outer'(?'inner'"(?:\\.|[^"])*"|'(?:[^']|'')*'|[^()]*|\((?P>inner)*\))*)\)/
    

    Note that if can avoid such regular expressions, you should do it (because you depend on re compiler too much), so I suggest to use vim motions directly:

    let s:reply=""
    function! SetReplyToKeywordArgs(...)
        let [sline, scol]=getpos("'[")[1:2]
        let [eline, ecol]=getpos("']")[1:2]
        let lchar=len(matchstr(getline(eline), '\%'.ecol.'c.'))
        if lchar>1
            let ecol+=lchar-1
        endif
        let text=[]
        let ellcol=col([eline, '$'])
        let slinestr=getline(sline)
        if sline==eline
            if ecol>=ellcol
                call extend(text, [slinestr[(scol-1):], ""])
            else
                call add(text, slinestr[(scol-1):(ecol-1)])
            endif
        else
            call add(text, slinestr[(scol-1):])
            let elinestr=getline(eline)
            if (eline-sline)>1
                call extend(text, getline(sline+1, eline-1))
            endif
            if ecol<ellcol
                call add(text, elinestr[:(ecol-1)])
            else
                call extend(text, [elinestr, ""])
            endif
        endif
        let s:reply=join(text, "\n")
    endfunction
    function! GetKeywordArgs()
        let winview=winsaveview()
        keepjumps call search('someKeyword', 'e')
        setlocal operatorfunc=SetReplyToKeywordArgs
        keepjumps normal! f(g@i(
        call winrestview(winview)
        return s:reply
    endfunction
    

    You can use something like

    let savedureg=@"
    let saved0reg=@0
    keepjumps normal! f(yi(
    let s:reply=@"
    let @"=savedureg
    let @0=saved0reg
    

    instead of operatorfunc to save and restore registers, but the above code leaves all registers and marks untouched, what I can't guarantee with saved* stuff. It also guarantees that if you remove join() around text, you will save information about the location of NULLs (if you care about them, of course). It is not possible with registers variant.

    0 讨论(0)
提交回复
热议问题