Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Search vi搜索副本粘贴搜索副本_Search_Vim_Copy_Vi_Paste - Fatal编程技术网

Search vi搜索副本粘贴搜索副本

Search vi搜索副本粘贴搜索副本,search,vim,copy,vi,paste,Search,Vim,Copy,Vi,Paste,我只是想知道是否有人可以帮助我们如何使用vi来完成下面的工作 我有一个文本文件,它可能包含如下内容 start of text file: --something1.something2-- --anotherThing1.something2-- end of text file: 如果我想获取此行并通过搜索与第一次出现的[A-Za-z0-9]匹配的任何内容将其转换,请将其复制到缓冲区,然后在第一次出现之前将其追加到同一行-- 是否有一个VI命令来执行此操作 干杯 本 或者每次更换都不要求确

我只是想知道是否有人可以帮助我们如何使用vi来完成下面的工作

我有一个文本文件,它可能包含如下内容

start of text file:
--something1.something2--
--anotherThing1.something2--
end of text file:
如果我想获取此行并通过搜索与第一次出现的
[A-Za-z0-9]
匹配的任何内容将其转换,请将其复制到缓冲区,然后在第一次出现之前将其追加到同一行--

是否有一个VI命令来执行此操作

干杯 本

或者每次更换都不要求确认:

:%s/--\([a-zA-Z0-9]*\).\(.*\)--/--\1.\2.\1--/g
将产生:

--something1.something2.something1--
--anotherThing1.something2.anotherThing1--
发件人:

如果要将'--'之后的第一个单词复制到第一个'.'并附加'.'和在最后一个'--'之前找到的单词,则需要执行此操作

使用vim

重新评论:

有人提到,如果有多个单词等等,它将不起作用。 我在以下方面进行了测试:

start of text file:
--something1.something2.something3.something4.something5.something6--
--anotherThing1.something2.anotherThing3.anotherThing4.anotherThing5--
end of text file:
替换为上述表达式后:

start of text file:
--something1.something2.something3.something4.something5.something6.something1--
--anotherThing1.something2.anotherThing3.anotherThing4.anotherThing5.anotherThing1--
end of text file:

天哪,在我登录发布答案的时候,你发布了答案,并且已经收到了评论

%s/--\(\w*\)\.\(.*\)--/--\1.\2.\1--/g
--ab1.cd2..yz99-->--ab1.cd2..yz99.ab1--

试试这个:

%s:\([A-Za-z0-9]\+\)\(\..\+\)--:\1\2.\1--:g

基于stefanB的解决方案,但使用否定字符类和“非常神奇”设置,我得到了以下替换命令:

:%s/\v--([^.]+)\.(.*)--/--\1.\2.\1--/
根据具体要求(“something1”和“anotherThing1”允许使用哪些字符),这可能更正确,也可能不正确


还有一件事需要考虑:到目前为止发布的所有解决方案都假定每行只出现一个“--text.someOtherText”--模式。如果不是这样,则(.*))必须调整部分模式,并且需要/g修饰符。

因此,您要复制'--'up to first'之后的第一个单词,并附加'.'和在最后一个'--'之前找到的单词?请删除“c”“如果您不想被要求确认每次替换,则从结尾开始。不需要对正则表达式进行/g修改。如果一行上有多个--text.text-,您的正则表达式不起作用。您所说的--text.text-…,是什么意思。。。你们有失败的例子吗?你们是一颗真正伟大的炸弹。我通常会做一些不同的替换g/--([A-Za-z0-9]*)。(.*)-/s/--\1.\2.\1--真的很喜欢这些小组不确定你能不能做到这一点,谢谢你的帮助,我实际上已经用多个片段进行了测试,比如”--另一个1.另一个1.另一个1.另一个1--它仍然会在最后一个单词之前添加第一个单词--
%s/--\(\w*\)\.\(.*\)--/--\1.\2.\1--/g
%s:\([A-Za-z0-9]\+\)\(\..\+\)--:\1\2.\1--:g
:%s/\v--([^.]+)\.(.*)--/--\1.\2.\1--/