1. Technology

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

By

5 of 10

"I never metacharacter I didn't like."

As the saying goes, "I never metacharacter I didn't like." Once you learn how to use metacharacters, you will wonder how you ever got on without them. This is particularly true in Python. Python has a robust regular expression engine and employs a wide array of metacharacters. The following is a list of the symbols and their uses:

  • .: any character except a newline.
  • ^: the boundary at the beginning of a character string (the green line)
  • $: the boundary at the end of a character string (the other green line)
  • *: zero or more instances of a pattern
  • +: one or more instances of a pattern
  • ?: one or more instances of a pattern
  • \: the indicator of an escape sequence
  • {}: used to specify parameters for the matching of the regular expression
  • []: indicates a set of characters for a single position in the regex
  • (): used to group regular expressions
  • |: used as an 'or' between two possible matches.
These are the extended alphabet of regular expression formulation. Many of them are a simple 1-to-1 symbolic meaning (".", "^", "$", "*", "+", "?", "|", "()" and "[]"). But two take on a life of their own when placed in conjunction with certain letters of the alphabet. In the next section, we will look at these compound symbols and their meanings.

  1. About.com
  2. Technology
  3. Python
  4. Regular Expressions
  5. Python Regular Expressions - Python Regex - Python says: "I never metacharacter I didn't like.": Metacharacters in Python Regular Expression Handling

©2014 About.com. All rights reserved.