There is a table at http://www.regular-expressions.info/posixbrackets.html that summarizes all the POSIX bracket expressions and also provides the equivalent shorthand.
I am unable to understand why this doesn't mention \S
as a shorthand for [:graph:]
. Are they different? If yes, then could you please explain me, with examples, how they are different?
\s -- (lowercase s) matches a single whitespace character -- space, newline, return, tab, form [ \n\r\t\f]. \S (upper case S) matches any non-whitespace character. \t, \n, \r -- tab, newline, return. \d -- decimal digit [0-9] (some older regex utilities do not support \d, but they all support \w and \s)
In regex, the uppercase metacharacter is always the inverse of the lowercase counterpart. \d (digit) matches any single digit (same as [0-9] ). The uppercase counterpart \D (non-digit) matches any single character that is not a digit (same as [^0-9] ).
By default, regular expressions will match any part of a string. It's often useful to anchor the regular expression so that it matches from the start or end of the string: ^ matches the start of string. $ matches the end of the string.
In a regular expression, if you have [a-z] then it matches any lowercase letter. [0-9] matches any digit. So if you have [a-z0-9], then it matches any lowercase letter or digit. You can refer to the Python documentation for more information, especially in the chapter 6.2-Regular Expression operations.
[:graph:]
is different character class from \S
.
[:graph:]
only match visible characters. But \S
match any characters that is not space (space, newline, character return, line feed, tab, vertical tab, ..).
For example, [:graph:]
does not match NUL, Backspace, BEL, ..., but \S
match them.
Python example using regex
package (which support 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']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With