Regex 一行中所有匹配项的开始列和结束列的列表
我试图创建一行中所有匹配项的开始列列表和结束列列表 这就是我所做的: 第1行:Regex 一行中所有匹配项的开始列和结束列的列表,regex,list,vim,match,lines,Regex,List,Vim,Match,Lines,我试图创建一行中所有匹配项的开始列列表和结束列列表 这就是我所做的: 第1行: test is PASSED as its the list "that" is [passed] as let MatchesStart = [] | 1s/\<[a-zA-Z\x7f-\xff]\+\>/\=add(MatchesStart,match(getline(1),submatch(0)))/gn let MatchesEnd = [] | 1s/\<[a-zA-Z\x7f-\
test is PASSED as its the list "that" is [passed] as
let MatchesStart = [] | 1s/\<[a-zA-Z\x7f-\xff]\+\>/\=add(MatchesStart,match(getline(1),submatch(0)))/gn
let MatchesEnd = [] | 1s/\<[a-zA-Z\x7f-\xff]\+\>/\=add(MatchesEnd,matchend(getline(1),submatch(0)))/gn
搜索:\
(查找所有单词) 命令:
test is PASSED as its the list "that" is [passed] as
let MatchesStart = [] | 1s/\<[a-zA-Z\x7f-\xff]\+\>/\=add(MatchesStart,match(getline(1),submatch(0)))/gn
let MatchesEnd = [] | 1s/\<[a-zA-Z\x7f-\xff]\+\>/\=add(MatchesEnd,matchend(getline(1),submatch(0)))/gn
让MatchesStart=[]|1s/\/\=add(MatchesStart,match(getline(1),submatch(0))/gn
让MatchesEnd=[]| 1s/\/\=add(MatchesEnd,matcheend(getline(1),submatch(0))/gn
结果:
test is PASSED as its the list "that" is [passed] as
let MatchesStart = [] | 1s/\<[a-zA-Z\x7f-\xff]\+\>/\=add(MatchesStart,match(getline(1),submatch(0)))/gn
let MatchesEnd = [] | 1s/\<[a-zA-Z\x7f-\xff]\+\>/\=add(MatchesEnd,matchend(getline(1),submatch(0)))/gn
MatchesStart=[0,5,8,9,18,22,26,32,5,8,9]MatchesEnd=[4,7,14,11,21,25,30,36,7,14,11] 问题是,匹配项不是唯一的。行中的最后一个单词“as”在“Passed”中找到,最后一个单词“is”未找到,因为它被检测为行中的第二个单词,依此类推 预期产出: MatchesStart=[0,5,8,15,18,22,26,32,38,42,50]
MatchesEnd=[4,7,14,17,21,25,30,36,40,48,52] 请查看条目4、9、10和11的差异 如何创建唯一的匹配数列表(从行首到行尾) 更新:
test is PASSED as its the list "that" is [passed] as
let MatchesStart = [] | 1s/\<[a-zA-Z\x7f-\xff]\+\>/\=add(MatchesStart,match(getline(1),submatch(0)))/gn
let MatchesEnd = [] | 1s/\<[a-zA-Z\x7f-\xff]\+\>/\=add(MatchesEnd,matchend(getline(1),submatch(0)))/gn
请搜索\s\zs\p\w\+
为了找到匹配的开始和结束列,我尝试了match/matchend
匹配='31'matchend='-1' Searchpos提供了正确的startcolumn(32),但没有提供endcolumn。
无法使用matchend查找endcolumn,因为起始值错误是“\zs”之前的“\s”的原因。使用
searchpos()
函数搜索模式,将其包装在循环中(例如)。如果返回列表中的第二个元素为0,则已在该行上执行搜索
:h searchpos(
阅读详细信息。我并不完全清楚您期望的输出。每个单词的开头和结尾?您最终的目标是什么?@Bernhard,我更新了我的问题。searchpos()不是吗仅搜索起始位置?@Remonn您要搜索的内容取决于模式。您可以搜索\>
,然后搜索-1。@Remonn我保留我的答案和注释。同样,您要匹配的内容完全取决于模式,即正则表达式。如果您让正则表达式匹配..\>
,则searchpos()返回的值为-1将是您想要的数字。我想找到一种方法,在一行中插入所有匹配的开始列和结束列,无论我做什么搜索。@Remonn太好了!我希望您能尽快完成脚本/插件!