Using \d in regex range

Viewed 93

In neovim (v0.5.0), the regex \d matches any digit in the input string. The regex [0-9] does the same, but [\d] instead matches a literal d character, as seen in these images:

\d

[0-9]

[\d]

Is it possible to use \d in a character range like that, or do I have to type [0-9]?

1 Answers

Inside a POSIX bracket expression ([...]), \d cannot be used to match digits.

However, inside a POSIX bracket expression, you can use POSIX character classes. Neovim supports quite a few, but in your case, you can use [:digit:]. So, to match either a comma or digits, you can use [,[:digit:]].

In Neovim, you can switch to an NFA regex engine that supports shorthand character classes inside square brackets.

Vim will automatically select the right engine for you. However, if you run into a problem or want to specifically select one engine or the other, you can prepend one of the following to the pattern:

      \%#=0 Force automatic selection. Only has an effect when
            'regexpengine' has been set to a non-zero value.
      \%#=1 Force using the old engine.
      \%#=2 Force using the NFA engine.

So, you can use [\d] after you prepend the pattern with \%#=2.

Related