Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the length of a regex match in vim?

Tags:

regex

vim

If I want to get the length of each match within the parentheses in the following regex, how do I do it?:

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

I'm trying to modify 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 the regular expression to find the current width of each column.

like image 676
misfo Avatar asked Oct 24 '25 18:10

misfo


1 Answers

Well, how do you want to capture it?

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

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

\= lets you substitute the result of a vimscript expression for a matching string. submatch(0) is the string matched (submatch(n) would be the nth group).

like image 109
rampion Avatar answered Oct 26 '25 09:10

rampion