1. Home
  2. Computing & Technology
  3. Python

Pythons 're' Module: Regular Expression Objects

Methods and Attributes of Regular Expression Objects

By Al Lukaszewski, About.com

Using the compile() function, one compiles a regular expression into a regular expression object and (usually) assigns it to a value. The methods and attributes of such regular expression objects are as follows ("regex" is here used as the object handler):

  • regex.match(string[, start-position [, end-position]]): Similar to the match() function but allows you to indicate a start position and an end position for the matching.
  • regex.search(string [, start-position] [, end-position]): Similar to the search() function but allows the limitation of searching to be within two points within string.
  • regex.split(string[, maxsplit = 0]): Same as the split() function.
  • regex.findall(string[, start-position [, end-position]]): Identical to the findall() function except that it allows limitations within the string.
  • regex.finditer(string[, start-position[, end-position]]): Identical to the dall() function except that it allows limitations within the string.
  • regex.sub(replacement, string [, count = 0]): Same as the sub() function.
  • regex.subn(replacement, string [, count = 0]): Same as the subn() function.
  • regex.flags: The flags used when compiling the regular expression object.
  • regex.groupindex: A dictionary mapping of any symbolic group names defined by r'(?P<id>' to group numbers. If no symbolic groups were used in the pattern, the dictionary is empty.
  • regex.pattern: The pattern from which the regular expression object was compiled.

Explore Python

More from About.com

  1. Home
  2. Computing & Technology
  3. Python
  4. Regular Expressions
  5. Python Library - Python Regular Expressions - RE Object Methods and Attributes

©2008 About.com, a part of The New York Times Company.

All rights reserved.