1. Technology

Pythons 're' Module

're' Functions


London Python Code Dojo Feb 2011
R4vi/Flikr/CC BY 2.0

Python's re module is without a doubt one of the most commonly used parts of the Python library. Remember its functions, on the other hand, is not always easy. Here As always, it is always better to know all of the tools at your disposal when you code.

The following is a reference for the re module's functions. It does not include the metacharacter sequences. If you want to see some of them in action, be sure to see the list of Python regex examples. The flags indicated in some methods are optional ways of nuancing the matching capacity of the regular expression. In the section "Regular Expressions", you will find them defined and exemplified.

  • compile(string [, flags]): Compiles a regular expression pattern called string into a regular expression object. That object also has several methods and attributes associated with it.
  • search(pattern, string [, flags]): Searches for the first instance of pattern. If it is successful, a MatchObject is returned, otherwise the value "None".
  • match(pattern, string [. flags]): Checks for a match at the beginning of string, returning the MatchObject with success or "None" on failure.
  • split(pattern, string [, maxsplit = 0]): Splits a string using pattern as a delimiter. This allows you to split a string on the occurrence of multi-character entities such as words, figures, and combinations of the two. The optional maxsplit limits the number of items that will be returned. All possible splitting is rendered by default.
  • findall(pattern, string [, flags]): Returns a list of non-overlapping matches of pattern found in string. If grouping is used, the matching text of the group is returned.
  • finditer(pattern, string [, flags]): Operates similarly to findall except that it returns an iterator. This saves system resources by returning only one of the matches at a time.
  • sub(pattern, replacement, string [, count = 0]): Substitutes replacement for the left-most, non-overlapping occurence of pattern in string. By default, all substitutions are rendered, but these can be limited by using count. Returns the new string.
  • subn(pattern, replacement, string [, count = 0]): Performs the same task as sub() but returns a tuple comprised of the new string and the number of substitutions rendered.
  • escape(string): Performs an automatic substitution, substituting escaped versions of all non-alphanumeric elements in string.
  1. About.com
  2. Technology
  3. Python
  4. Regular Expressions
  5. Python Library - Python Regular Expressions - RE module Functions

©2014 About.com. All rights reserved.