regex (vim) for print ... to print(...) for python2 to python3

varantir

This post is helpful only if you have strings inside of the print command. Now I have tons of sourcecode with a statement such as

print milk,butter

which should be formatted to

print(milk,butter)

And capturing the end of the line with \n was not sucessfull. Any hints?

benjifisher

I am not familiar with 2to3, but from all the comments, it looks like the correct tool for the job.

That said, perhaps we can use this question as an excuse for a short lesson in some vim basics.

First, you want a pattern that matches the correct lines. I think that ^\s*print\> will do:

  • ^ matches start of line (and $ matches end of line).
  • \s matches whitespace (space or tab)
  • * means 0 or more of the previous atom (as many as possible, or "greedy").
  • print is a literal string.
  • \> matches end-of-word (zero width). You might use a (literal) space or \s\+ instead.

Next, you need to identify the part to be enclosed in parentheses. Since * is greedy, .* will match to the end of the line; there is no need to anchor it on the right. Use \(\s*print\) and \(.*\) to capture the pieces, so that you can refer to them as \1 and \2 in the replacement.

Now, put the pieces together. There are many variants, and I have not tried to "golf" this one:

:%s/^\(\s*print\)\s\+\(.*\)/\1(\2)

Some people prefer the "very magic" version, where only a-z, A-Z, 0-9, and _ are treated as literal characters; then you do not need to escape the parentheses nor the plus:

:%s/^\v(\s*print)\s+(.*)/\1(\2)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

regex (vim) for print ... to print(...) for python2 to python3

From Dev

print in python using python3 without python2 and python1 or both at the same time?

From Dev

print function in Python3

From Dev

Python3, a lazy print

From Dev

How to print regex match results in python 3?

From Java

How to print like printf in Python3?

From Dev

python3 string "abcd" print: aababcabcd?

From Dev

format r(repr) of print in python3

From Dev

Print multidimensional list in python3

From Dev

python3 remove space from print

From Dev

Python: How to print a regex matched string?

From Dev

trying to print a group from a regex match in python

From Dev

How to print substring using RegEx in Python?

From Dev

RegEx Python Find and Print to a new document

From Dev

Python Regex Script sort and print to new document

From Dev

python 3 print generator

From Dev

Python 3 print() to a variable

From Dev

Python3 - I do not want to print the Javascript code with BeautifulSoup

From Dev

How to print() a string in Python3 without exceptions?

From Dev

What is the role of "*" inside of a Python3 print function?

From Dev

Python3 Print is not overwriting current line in some terminals

From Dev

What is the role of "*" inside of a Python3 print function?

From Dev

Use codec error handler for print() in Python3?

From Dev

Python3 - Loop over rows, then some columns to print text

From Dev

Pycharm recognizes kwargs in print as wrong (python3)

From Dev

Python3 UnitTests: How to print to stdout out

From Dev

python3 checkers board won't print

From Dev

Python3 Multi-Lined String Print Next

From Dev

how print parse-tree using python2 runtime with antlr4

Related Related

HotTag

Archive