Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
742 views
in Technique[技术] by (71.8m points)

vim regex for python2 print to python3

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...