vim regex for python2 print to python3

百般思念 提交于 2020-01-10 05:38:26

问题


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

print 'asdf'
print 'hi mom!'

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

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

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.


回答1:


This would work for your examples

:%s/print \('.*'\)/print(\1)/g
  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.

But I also recommend using 2to3



来源:https://stackoverflow.com/questions/17871086/vim-regex-for-python2-print-to-python3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!