This course will become read-only in the near future. Tell us at community.p2pu.org if that is a problem.

Regular Expressions [July 16, 2012, 5:55 a.m.]



In computing, a regular expression, also referred to as regex or regexp, provides a concise and flexible means for matching strings of text, such as particular characters, words, or patterns of characters.

The following examples illustrate a few specifications that could be expressed in a regular expression:

  • The sequence of characters "car" appearing consecutively in any context, such as in "car", "cartoon", or "bicarbonate"
  • The sequence of characters "car" occurring in that order with other characters between them, such as in "Icelander" or "chandler"
  • The word "car" when it appears as an isolated word
  • The word "car" when preceded by the word "blue" or "red"
  • The word "car" when not preceded by the word "motor"
  • A dollar sign immediately followed by one or more digits, and then optionally a period and exactly two more digits (for example, "$100" or "$245.99").
Source: Wikipedia

Python regular expression syntax follows in the Perl lineage. The Python module re provides regular expression functionality. Regular Expressions are a sub-language embedded within the larger Python language.
 

Educational Resources

Python for Informatics

Dive Into Python 3

Python.org

Practice

Please post regex excercises and questions below. We can help each other learn and explore this robust and slightly difficult aspect of Python.