Character Classes (Shorthand) Parent topic

Character Classes (shorthand)

Element
What It Means
Example
\d
Any digit character; functionally equivalent to [0-9] or [[:digit:]]
\d matches 1, 12, 123, etc., but not 1b7. One or more of any digit characters.
\D
Any non-digit character; functionally equivalent to [^0-9] or [^[:digit:]]
\D matches a, ab, ab&, but not 1. One or more of any character but 0, 1, 2, 3, 4, 5, 6, 7, 8, or 9.
\w
Any "word" character. That is, any alphanumeric character; functionally equivalent to [_A-Za-z0-9] or [_[:alnum:]]
\w matches a, ab, a1, but not !&. One or more upper- or lower-case letters or digits, but not punctuation or other special characters.
\W
Any non-alphanumeric character; functionally equivalent to [^_A-Za-z0-9] or [^_[:alnum:]]
\W matches *, &, but not ace or a1. One or more of any character but upper- or lower-case letters and digits.
\s
Any white space character; space, new line, tab, non-breaking space, etc.; functionally equivalent to [[:space]]
vegetable\s matches "vegetable" followed by any non-white space character. So the phrase "I like vegetables in my soup" would trigger the regex, but "I like a vegetable in my soup" would not.
\S
Any non-white space character; anything other than a space, new line, tab, non-breaking space, etc.; functionally equivalent to [^[:space]]
vegetable\S matches "vegetable" followed by any non-white space character. So the phrase "I like vegetables in my soup" would trigger the regex, but "I like a vegetable in my soup" would not.