python2的vim正则表达式打印到python3 [英] vim regex for python2 print to python3

查看:52
本文介绍了python2的vim正则表达式打印到python3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以说我有一个 python v2.7 文件,里面有一些这样的代码:

So say I have a python v2.7 file with some code like this:

print 'asdf'
print 'hi mom!'

但我想在 python3 中运行它,我需要像这样将这些括号添加到它们中:

But I want to run it in python3, I'll need to add those parenthesis to them like so:

print('asdf')
print('hi mom!')

我试图在 vim 中使用以下正则表达式来解决问题,但它不起作用:

I was trying to use the following regex in vim to solve the problem, but it wasn't working:

:%s/print\ '.*'/print('\1')/gc

它只是给了我带有空字符串的打印函数(带括号).任何帮助表示赞赏;谢谢.

It just gave me print functions (with parenthesis) that had empty strings. Any help is appreciated; thanks.

推荐答案

这适用于您的示例

:%s/print \('.*'\)/print(\1)/g

  1. 您无需逃离空间.
  2. 您实际上并未在括号中捕获任何内容,因此 \1 在您的正则表达式中是一个空字符串.
  1. You don't need to escape the space.
  2. You don't actually capture anything in parenthesis so the \1 is an empty string in your regex.

但我也推荐使用 2to3

这篇关于python2的vim正则表达式打印到python3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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