2

I studied a tutorial for vim syntax files and found this example:

" Floating point like number with E and no decimal point (+,-)
syn match celNumber '[-+]\=\d[[:digit:]]*[eE][\-+]\=\d\+'

I was surprised that it uses both \d and [:digit:]. I thought they mean the same thing. The vim help for pattern says:

Character classes:
      magic   nomagic   matches ~
...
\d    \d      \d        digit:    [0-9]

And for [:...:] character classes:

- A character class expression is evaluated to the set of characters
  belonging to that character class.  The following character classes
  are supported:
Name          Func         Contents ~
...
[:digit:]     [:digit:]    decimal digits '0' to '9'

So what is the difference? Can't the example pattern \d[[:digit:]]* be simplified to \d\+?

0

1 Answer 1

4

There is no difference in which symbols they encompass; both denote the digits from 0 to 9. They only differ in where they can resp. must be used. \d can not be used inside a collection, i.e., /[\d]/ will search for backslashes and lower case Ds, but not for digits. In contrast to this, [:digit:] must be used inside a collection, i.e., only /[[:digit:]]/ will search for a digit -- /[:digit:]/ would search for one of the letters d, g, i, t or for a colon.

And yes, \d[[:digit:]]* can be simplified to \d\+.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.