Chapter 4: Boundaries

Special characters

face Josiah Wang

Here are also some special characters that can be used as a shorthand for the corresponding regular expressions:

  • \d == [0-9] (“digits”)
  • \D == [^0-9] (“non-digits”)
  • \s == [ \t\n\r\f\v] (“whitespace”)
  • \S == [^ \t\n\r\f\v] (“non-whitespace”)
  • \w == [a-zA-Z0-9_] (“word”)
  • \W == [^a-zA-Z0-9_] (“non-word”)
>>> re.match("\d+.\d{2}", "10.24") 
<re.Match object; span=(0, 5), match='10.22'>
>>> re.match("\S+\s\S+", "Love Python") 
<re.Match object; span=(0, 11), match='Love Python'>