In POSIX, why does a character class such as [[:digit:]]
have double brackets? Do the outer and inner brackets mean the same? Thanks?
A character class defines a set of characters. Saying — "match one character specified by the class". [:digit:]
is a POSIX character class and [ ... ]
is a bracket expression here.
The POSIX class notation is only valid inside a bracketed expression. For example,
[:digit:]
, when not inside a bracketed expression, will not be read as the POSIX named class. Rather, in most flavors it is the character class containing the characters (:
, d
, i
, g
, t
) literally.
The outer brackets indicate that any character enclosed with match. The [:digit:]
is the POSIX "any digit" character class. For example [[:digit:][:alpha:]]
says "any digit or any alphabetical character". In ASCII, [[:digit:][:alpha:]]
is equivalent to [0-9a-zA-Z]
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With