Is [: graph:] equivalent to \ S in regular expressions?

There is a table that summarizes all the POSIX parenthetical expressions and also provides an equivalent shorthand version.

I can't figure out why it doesn't mention it \S

as a shorthand for [:graph:]

. Are they different? If so, could you please explain to me, with examples, how they differ?


source to share

1 answer


is a different character class from \S



match only visible characters. But it \S

matches any characters that are not a space (space, newline, return character, string, tab, vertical tab, ..).

For example, [:graph:]

does not match NUL, Backspace, BEL, ..., but does \S


Python example using a regex

package (which supports POSIX character classes):

>>> import regex
>>> regex.findall(r'[[:graph:]]', 'a \0 \a \b z')
['a', 'z']
>>> regex.findall(r'\S', 'a \0 \a \b z')
['a', '\x00', '\x07', '\x08', 'z']




All Articles