Vim reg expressions and alias methods

I study vim and start to see how strong it is (especially when combined with reg expressions). Right now I am trying to replace all integers in a python file with floating values. For instance. you will come across things like [0, 0, 0], which should be [0.0, 0.0, 0.0]

The best I have now::.% S / \ (\ d + \) \ @ <= S *, / 0, / g although I get results like 0.032.0

Two things:

  • Can any of you regexperts crack a reliable way to solve the above problem?
  • I am starting to see how complex these expressions are. Do I have to say a subregex definition, for example, an integer or float in my .vimrc, and you have a way to embed it in subsequent calls. for example, the required call could be:% s / \ ($ (AnInt) \) + / - / g to change the plus signs after integers in minuses. (This or something equally modular)
+4
source share
1 answer

Acting under the assumption that the results you get "as 0.032.0" are related to the fact that you started with 0.032

Caution, this is what happens when you try to force regular expressions and don't parse irregular languages ​​anything else. There are numerous edge cases where this regular expression fails. I will try to illustrate them when I pass.

:%s/\(\[.*\)\@<=\([0-9]\+\)\(,[^,]*\.[^,]*\)\@<!\.\@!\(.*\]\)\@=/\2.0/cg

How it works?

First, I want to make sure that I am inside an array. \(\[.*\)\@<=

This is a lookbehind statement for finding a literal [

This will lead to a failure in the lists that are indicated on several lines, as well as in such cases (they will correspond to 3 and 4):

 for (i,j,k) in zip([0, 0, 0], range(3), listOfLists[4]): 

This makes a compromise for complexity. Please double check all matches to make sure that you are not doing what you do not intend.

Next, I want to actually match the text, \([0-9]\+\) simple.

Next, I want to make sure that there are no periods before or after numbers, but ignore things before and after commas. \(,[^,]*\.[^,]*\)\@<!

Next, I want a literal period after the match. \.\@!

Finally, I want to make sure that there is literal ] after the match (to make sure I'm inside the array. \(.*\]\)\@=

And last but not least, replace the text found with the second captured group and add a period.

As for your # 2, no, but you can pull these areas into named buffers and use them as you wish. Use q: command editing mode q:

+4
source

All Articles