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

There is a table http://www.regular-expressions.info/posixbrackets.html 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?

+3


source to share


1 answer


[:graph:]

is a different character class from \S

.

[:graph:]

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

      

+5


source







All Articles