Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.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
Regex 我有一个文件有这么多行,需要在shell中查找和替换_Regex_Shell_Vim - Fatal编程技术网

Regex 我有一个文件有这么多行,需要在shell中查找和替换

Regex 我有一个文件有这么多行,需要在shell中查找和替换,regex,shell,vim,Regex,Shell,Vim,我有这样的台词 SC2268,Registration lauch causes a menu of what the user wants to do 我需要把这个换成 [SC2268]:Registration lauch causes a menu of what the user wants to do 我试过以下几件事: 1.%s/\(\w\+\),\(\w\+\)/\[1]:\2/gi 2.%s/\(SC[0-9]*\)/\[1]/gi 但是没有运气 :%s/^\([^,]*\

我有这样的台词

SC2268,Registration lauch causes a menu of what the user wants to do
我需要把这个换成

[SC2268]:Registration lauch causes a menu of what the user wants to do
我试过以下几件事:

1.%s/\(\w\+\),\(\w\+\)/\[1]:\2/gi
2.%s/\(SC[0-9]*\)/\[1]/gi
但是没有运气

:%s/^\([^,]*\),/[\1]:/

对我来说很合理(编辑:感谢@Sven指出我需要将
更改为

你很接近了。插入第一个匹配项的符号是
\1
;反斜杠必须直接位于数字之前:

:%s/\(SC[0-9]*\)/[\1]/gi
:s/{pattern}/{replacement}/
中的
{pattern}
{replacement}
部分的转义和语法是不同的!如有疑问,请咨询帮助:

  • :帮助模式
  • :帮助s/\1

还请注意,对于这个特殊的方法,有较短的方法;请看(并试着理解)@bobbogo的答案。

看起来你的
\1
是“坏的”(
\[1
不一样)而且,你似乎在逃避……一切。所以你实际上是在寻找
后面跟一个单词字符,后面跟
),(
等等。你能纠正我的正则表达式吗?我很懒,我会这样做:echo'SC2268,Registration lauch会生成一个用户想要做什么的菜单'| perl-pe's/^/[/;s/,/]:/'[SC2268]:Registration lauch会导致一个用户想要做什么的菜单。我认为
缺少了。你搞定了它,我在逃逸字符时错过了。我想知道最短的模式是什么?结果是你需要将
也更改为
(cheers@Sven),所以我认为你的答案可能是正确的。