none
none

Reputation: 31

This regex cannot match strings in vim

In order to substitute some columns of a table written in latex in vim, I write a regex to help me perform this work. The regex is:

s/\v^(\d+ &)( \d+ &)( \d+ &)/\1\3\2/g

And I want it to match the lines like these below:

10 & 25 & 25 & 100\\
7 & 56 & 56 & 100\\

However, this regex never works. Vim keeps telling me this pattern could not be found. But I have already used the very magic mode and this regex does work elsewhere, see https://regex101.com/r/DBwTHM/1.

Could anyone help me please? Thanks a lot!

Upvotes: 0

Views: 68

Answers (2)

nrz
nrz

Reputation: 10580

In Vim's "very magic" mode you need to escape each ampersand & with a backslash \.

:%s/\v^(\d+ \&)( \d+ \&)( \d+ \&)/\1\3\2/g

Upvotes: 3

romainl
romainl

Reputation: 196886

I've always found "verymagic" too troublesome.

:s/^\(\d\+\) & \(\d\+\) & \(\d\+\)/\1 \& \3 \& \2

Upvotes: 1

Related Questions