How to get regex length in vim?

If I want to get the length of each match in parentheses in the following regular expression, how to do this ?:

^\(\-\+\s\)\+

I am trying to change the width of columns in a buffer with data that is laid out as a table. Since the first two rows of the table will look like this:

 DESIGN_ID DESIGN_YEAR SOURCE_REFERENCE
---------- ----------- ----------------

I want to use regex to find the current width of each column.

+3
source share
1 answer

Well, how do you want to capture him?

This will put it at the beginning of all the relevant lines:

:%s/^-\+\%(\s-\+\)*\s\?$/\=strlen(submatch(0)) . ': '. submatch(0)

\=allows you to substitute the result of a vimscript expression for the corresponding string. submatch(0)matches the string ( submatch(n)will be the nth group).

+5
source

Source: https://habr.com/ru/post/1740203/


All Articles