Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are there double brackets in character classes in POSIX?

Tags:

regex

In POSIX, why does a character class such as [[:digit:]] have double brackets? Do the outer and inner brackets mean the same? Thanks?

like image 959
Tim Avatar asked Feb 22 '15 22:02

Tim


2 Answers

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.

like image 171
hwnd Avatar answered Nov 15 '22 05:11

hwnd


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].

like image 30
D.Shawley Avatar answered Nov 15 '22 04:11

D.Shawley