Cheatsheet

Metacharacters

  • .Any single character except newline
  • \dAny digit character (0-9)
  • \DAny non-digit character
  • \sAny whitespace character (space, tab, newline, etc.)
  • \SAny non-whitespace character
  • \wAny word character (letter, digit, underscore)
  • \WAny non-word character
  • \bWord boundary
  • \BNon-word boundary

Quantifiers

  • *Matches the preceding item zero or more times
  • +Matches the preceding item one or more times
  • ?Matches the preceding item zero or one time
  • {n}Matches the preceding item exactly n times
  • {n,}Matches the preceding item n or more times
  • {n,m}Matches the preceding item at least n times, but no more than m times

Anchors

  • ^Matches the start of the string
  • $Matches the end of the string
  • \bMatches a word boundary
  • \BMatches a non-word boundary

Groups

  • ()Matches the group inside the parentheses
  • (?:)Matches the group inside the parentheses, but doesn't capture the match
  • (?=)Positive lookahead. Matches the group inside the parentheses, but doesn't include it in the match
  • (?!)Negative lookahead. Matches if the group inside the parentheses doesn't match
  • (?<name>)Matches the group inside the parentheses and captures it using the specified name
  • (?P<name>)Matches the group inside the parentheses and captures it using the specified name (Python syntax)

Repetition

  • *Match 0 or more of the preceding character
  • +Match 1 or more of the preceding character
  • ?Match 0 or 1 of the preceding character
  • {n}Match exactly n of the preceding character
  • {n,}Match at least n of the preceding character
  • {n,m}Match at least n but not more than m of the preceding character

Escape Sequences

  • \dMatch any digit character (0-9)
  • \DMatch any non-digit character
  • \wMatch any word character (a-z, A-Z, 0-9, _)
  • \WMatch any non-word character
  • \sMatch any whitespace character (space, tab, newline, etc.)
  • \SMatch any non-whitespace character
  • \bMatch a word boundary
  • \BMatch a non-word boundary
  • \nMatch a newline character
  • \rMatch a carriage return character
  • \tMatch a tab character
  • \fMatch a form feed character
  • \vMatch a vertical tab character

Alternation

  • |Match either the expression before or after the | character
  • ( )Group expressions together to match as a unit
  • (?: )Group expressions together, but don't capture the group
  • (?= )Positive lookahead - Match if the expression inside the parentheses matches the input string, but don't include it in the match
  • (?! )Negative lookahead - Match if the expression inside the parentheses does not match the input string