Character classes

[ and ] are used for specifying a character class, which is a set of characters you wish to match. Beware, the characters in a class are OR-ed, not AND-ed! This means that the character class [abc] matches any of the characters a or b or c.
You can also use the shorthand notation like [a-c]. This notation is also possible for digits, like [0-3] matches character 0, 1, 2 or 3.

Character class
A character class defines a set of characters to be matched. The character class [abc] matches either char a, b or c. The characters in the class are OR-ed, not AND-ed. A shorthand notation would be [a-c].

 
Character classes behave in some special ways. Some special characters (also called metacharacters) lose their special nature inside a character class. Others don’t. And some ordinary characters actually gain special status inside a character class! The table below shows the impact of a character class on wildcards:

Character class impact on wildcards
[.] Loses special nature.
[\d] Keeps special nature.
[\D] Matches any non-digit.
[\s] Keeps special nature.
[\S] Keeps special nature.
[\w] Keeps special nature.
[\W] Keeps special nature.

The table below shows the impact on other metacharacters that we didn’t cover yet:

Character class impact on other special chars
[*] Loses special nature.
[+] Loses special nature.
[{m,n}] Loses special nature. The new interpretation is: matches { or m or , or n or }.
[|] Loses special nature.
[^] Changes its special nature. Outside character class: matches at the beginning of the string (or beginning of lines in multiline mode). Inside character class: if at the start (like [^5]), it negates all the other characters. If not at the start (like [4^5]), it is just an ordinary character like all the rest.
[$] Loses special nature.
[\A] Error: \A cannot be used inside a character class.
[\Z] Error: \A cannot be used inside a character class.
[\b] Gets a new meaning: [\b] is the backspace character!
[\B] Error: \B cannot be used inside a character class.