1. Technology

Python Regular Expressions (regex): Forming a Regular Expression in Python


6 of 10

Python's Compound Metacharacters

While many of the symbols mentioned on the previous page have a stand-alone meaning, two of them are used to form compound symbols and expressions which change the meaning of regular expressions. They are the backslash ("\") and curly braces "{}".

The backslash is the symbol used to initiate an escape sequence. Obviously, there are only so many characters on a keyboard and repetition of those basic components is necessary in any system. So the backslash is used to convert "normal" or "regular" characters into "escaped" characters. The following are the escaped characters that one can use within a regular expression and their meanings:

  • \n: a newline
  • \t: a tabspace
  • \A: the start of a string (similar to "^")
  • \Z: the end of a string (similar to "$")
  • \b: the boundary of a word (the red line)
  • \B: the empty string that is neither at the beginning nor at the end of a word
  • \d: any decimal digit (the mathematical set of real numbers)
  • \D: any non-decimal digit (the mathematical set of integers)
  • \s: any whitespace character (blank space, tab, etc.)
  • \S: any non-whitespace character
  • \w: any alphanumeric character and the underscore
  • \W: any non-alphanumeric character (e.g., "&", "£", "!", etc.) [\ul] The curly braces, however, take on a different meaning depending on their contents.

  1. About.com
  2. Technology
  3. Python
  4. Regular Expressions
  5. Python Regular Expressions - Python Regex - Pythonic Regular Expressions and Python's Compound Metacharacters

©2014 About.com. All rights reserved.