Home RegEx
Post
Cancel

RegEx

Metacharacters

  • .: Any character (except newline character)
  • ^: Starts with
  • $: Ends with
  • *: Zero or more occurrences
  • +: One or more occurrences
  • ?: Zero or one occurrences
  • {}: Exactly the specified number of occurrences
  • |: Either or
  • (): Capture and group

Special Sequences

  • \A: Returns a match if the specified characters are at the beginning of the string
  • \b: Returns a match where the specified characters are at the beginning or at the end of a word
  • \B: Returns a match where the specified characters are present, but NOT at the beginning (or at the end) of a word
  • \d: Any digit (0-9)
  • \D: Any non-digit character
  • \s: Any whitespace character
  • \S: Any non-whitespace character
  • \w: Any word character (a-z, A-Z, 0-9, _)
  • \W: Any non-word character
  • \Z: Returns a match if the specified characters are at the end of the string

Sets

  • [abc]: Will match any of the characters a, b, or c
  • [^abc]: Matches any character except a, b, or c
  • [a-z]: Matches any lowercase character
  • [A-Z]: Matches any uppercase character
  • [0-9]: Matches any digit
  • [a-zA-Z]: Matches any alphabet character (both lowercase and uppercase)
  • [0-9a-zA-Z]: Matches any alphanumeric character

Python Functions

  • re.findall(): Returns a list containing all matches
  • re.search(): Returns a Match object if there is a match anywhere in the string
  • re.split(): Returns a list where the string has been split at each match
  • re.sub(): Replaces one or many matches with a string
This post is licensed under CC BY 4.0 by the author.