Skip to main content


youtube.com/watch?v=cNbcRXKsXf…

OMG!! This one contains so much interesting information about vim and regex stuff 😮 Super interesting... also the problem itself that gets solved is a nice one!! 😺

#vim #neovim #regex

This entry was edited (1 year ago)
in reply to Rebeka Catalina

Instead of all that crazy macro stuff, you can just use a regex:

\(.\)\%(\1\@!.\1\@!.\1\@!\)\@=\(.\)\%(\2\@!.\2\@!\)\@=\(.\)\3\@!.<br>

Which is admittedly unreadable, so here it is with some extra formatting/comments:
\(.\)  # match and capture (in \1) any single character<br>  \%(  # group (but don't capture) ...<br>    \1\@!  # not-followed-by whatever is in \1 (i.e. what was matched by the first capturing group)<br>    .  # match/advance by any single character<br>    \1\@! .  # same thing: any single character that is not \1<br>    \1\@!  # same thing: ... not followed by \1 agin<br>  \)\@=  # ... end of group, positive lookahead: match any single character followed-by 3 characters that are different<br>\(.\)  # match and capture (in \2) another single character<br>  \%(  # group (but don't capture)<br>    \2\@! .  # any single character that is not \2<br>    \2\@!  # not followed by \2 again<br>  \)\@=  # ... end of group, positive lookahead<br>\(.\)  # match and capture (in \3) another single character<br>  \3\@!  # not followed by \3<br>.  # another single character<br>

In essence, we can use \(.\) followed by \1\@!. to say "match some character" followed by "some character that is different from the first one".

We use this to match the first character and ensure that the three next characters are different from the first one:

Axxx<br># where x is not equal to A<br>

The xxx check is in a look-ahead group, so it doesn't advance the overall match position, which means we can now check against the second character:
ABxx<br># where x is not equal to B<br># (and in round 1 we've already checked that none of them are equal to A)<br>

The same thing repeats for the third character:
ABCx<br># where x is not equal to C<br>

And the last character can just be matched by . because we've ensured that it is not equal to any of the first three.
This entry was edited (1 year ago)