Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim Regex column position matching

Tags:

regex

vim

I'm learning about the Vim Pattern, and really confused about the column position matching definition. Below I do very simple test. Just create a file, in the first line first column type: 123456789. Just make me easy to track the column number where each digit locate. Then I search /.\%>3c.*\%<8c, it matches 3456 and seems reasonable, because as the document explain \%<8c will match the 7th column and it's zero-width match so it will only match up to 6. But then I search /\%>3c.*\%<8c, this time Vim matches 4567. So why this time it matches 7??? It seems unreasonable. My Vim version is up-to-date: 7.4 Included patches: 1-884.

like image 327
Audison Avatar asked Nov 09 '22 03:11

Audison


1 Answers

If your aim is to search for column positions greater than 3 and less than 8, you don't need dots or stars, this suffices:

/\%>3\%<8

If you question is about the unintuitive behavior of adding both dots and/or stars to your search, then yes it is confusing. I believe in this case, the star is superfluous. You can get the same behavior you see (numbers 4-6 found) by just searching for:

/\%>3.\%<8

I believe the dot is considered a restrictive criteria for the search. In other words, there must be a char proceeding the column position. So the search routine runs something like this: is there a column position 4 with a character in 5th position? yes, add it to the result; is there is column position 6 with a char in position 7? yes, add it; is there a column position 7 with a char in 8th position? No -- because the zero-width criteria of not including 8th position or beyond (\%<8). To include the char in eight-position, you can add another dot after 8c and then 4-7 will be found, e.g.:

/\%3c.\%<8c.

But, note, this just gets us back to my first example:

 /\%>3\%<8 <=> /\%3c.\%<8c. 
like image 99
gregory Avatar answered Nov 15 '22 05:11

gregory