我有很多行一些Python代码类似的行的其余部分:正则表达式搜索和替换,包括括号
print "some text" + variables + "more text and special characters .. etc"
我想修改该把一切印刷后的括号内,就像这样:
print ("some text" + variables + "more text and special characters .. etc")
如何在vim中使用正则表达式来做到这一点?
我有很多行一些Python代码类似的行的其余部分:正则表达式搜索和替换,包括括号
print "some text" + variables + "more text and special characters .. etc"
我想修改该把一切印刷后的括号内,就像这样:
print ("some text" + variables + "more text and special characters .. etc")
如何在vim中使用正则表达式来做到这一点?
使用此替代:
%s/print \(.*$\)/print (\1)
\(.*$\)
匹配一切都交给了线的末端,并使用转义的括号捕获它在一组。替换包括使用\1
的该组,其中包含文字括号。
:%s/print \(.*\)/print(\1)/c
或者如果你直观地选择多行
:'<,'>s/print \(.*\)/print(\1)/c
%
- 每行
'<,'>
- 选择线路
s
- 替代
c
- 确认 - 告诉你什么是匹配你转换
前 print \(.*\)
- 完全匹配打印后跟一个空格N组的一切\(
和\)
print(\1)
之间 - 用打印代替(<第一场比赛>)
Vim有一些正则表达式规则的功能,你可以做:help substitute
或:help regex
,看看它们是什么。
以下链接可能对您有所帮助! –
2012-08-04 15:22:33