在vim中使用正则表达式匹配括号块 [英] Match parenthesised block using regular expressions in vim

查看:89
本文介绍了在vim中使用正则表达式匹配括号块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试匹配属于某个 ( 与其匹配的 ) 之间的内容,如使用动作 % 时由 vim 发现的>.

更具体地说,我正在寻找一个看起来像这个假设的 /someKeyword (\{pair}\(.*\))\{pair}/ 的正则表达式,如果有这样的修饰符作为 \{pair} ,当应用于正则表达式中的两个正好两个字符时,使第二个仅匹配与第一个匹配的括号(%-wise).

我正在寻找的模式应该与 someKeyword 后面的第一个括号的内部内容相匹配(注意它应该处理的代码总是正确地括号),如示例所示:

对于someKeyword ("aaa"),子匹配将匹配"aaa".同样,someKeyword("aaa)") 将匹配 "aaa)" 并且 someKeyword(("double-nested stuff")) 将匹配 <代码>(双重嵌套的东西")

但也适用于以下情况:

<代码>(someKeyword("xyz"))

它应该匹配"xyz"的地方.

有没有办法在正则表达式中使用vim的匹配括号功能?如果没有,还有什么其他解决方案可以实现这一目标?

Edit 1:匹配的内容可能跨越多行.

解决方案

这在 vim 正则表达式中是不可能的(因为允许这种嵌套结构的语言不是正则),但在 perl 提供的正则"表达式中是可能的(以及其他语言,我不太清楚)和 perl 可以在 vim 中使用.我不喜欢 vim-perl 绑定(因为它非常有限),但是如果您知道所有应该起作用的情况,那么您可以使用 perl 正则表达式的递归功能(需要更新的 perl,我有 5.12*):

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

注意,如果可以避免这样的正则表达式,你应该这样做(因为你太依赖re编译器了),所以我建议直接使用vim motions:

let s:reply=""功能!SetReplyToKeywordArgs(...)让 [sline, scol]=getpos("'[")[1:2]让 [eline, ecol]=getpos("']")[1:2]让 lchar=len(matchstr(getline(eline), '\%'.ecol.'c.'))如果 lchar>1让ecol+=lchar-1万一让文本=[]让 ellcol=col([eline, '$'])让 slinestr=getline(sline)如果 sline==eline如果ecol>=ellcol调用扩展(文本,[slinestr[(scol-1):],"])别的调用添加(文本,s​​linestr[(scol-1):(ecol-1)])万一别的调用添加(文本,s​​linestr[(scol-1):])让 elinestr=getline(eline)if (eline-sline)>1调用扩展(文本,getline(sline+1,eline-1))万一如果ecol<ellcol调用添加(文本,elinestr[:(ecol-1)])别的调用扩展(文本,[elinestr,"])万一万一让 s:reply=join(text, "\n")端功能功能!获取关键字参数()让 winview=winsaveview()keepjumps 调用 search('someKeyword', 'e')setlocal operatorfunc=SetReplyToKeywordArgs保持跳跃正常!f(g@i(调用 winrestview(winview)返回 s:reply端功能

你可以使用类似的东西

let savedureg=@"让saved0reg=@0保持跳跃正常!供参考(让 s:reply=@"让@"=savedureg让@0=saved0reg

而不是 operatorfunc 来保存和恢复寄存器,但上面的代码保留了所有寄存器和标记不变,我不能保证保存*的东西.它还保证,如果您删除 text 周围的 join(),您将保存有关 NULL 位置的信息(当然,如果您关心它们).使用寄存器变体是不可能的.

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

More specifically, I'm looking for a regex that looks like this hypothetical /someKeyword (\{pair}\(.*\))\{pair}/, if there were such modifiers as \{pair} that when applied to two exactly two characters in a regex, makes the second one only match if it's the matching bracket to the first one (%-wise).

The pattern I'm looking for should match the inner contents of the first bracket following someKeyword (n.b. the code that it should work on is always correctly bracketed), as in the examples:

For someKeyword ("aaa") the submatch will match "aaa". Likewise someKeyword ("aaa)") will match "aaa)" and someKeyword(("double-nested stuff")) will match ("double-nested stuff")

But also in cases like:

(
  someKeyword("xyz"))

where it should match "xyz".

Is there any way to make use of vim's matching bracket functionality in regexes? And if not, what other solution might work to accomplish this?

Edit 1: the matched contents may span several lines.

解决方案

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.

这篇关于在vim中使用正则表达式匹配括号块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆