1. Technology

Pythons 're' Module: RegEx MatchObjects

Methods and Attributes of MatchObjects

By

Hand with Computer Mouse
TommL/Vetta/Getty Images

When a regular expression matches a string, a match object is created: (called a MatchObject). These objects have the: following methods and attributes ("mo" = the object handler of the MatchObject[/i[):

  • mo.expand(template): If grouping is used, this method returns the pattern with the escape sequences, backreferences, and groups replaced by what they represent.
  • mo.group([group1, ...]): Returns the subgroups of a match.
  • mo.groups([default]): Returns a tuple that contains all the subgroups of a match. These are keyed by the subgroup name. "None" is returned for groups that do not match.
  • mo.groupdict([default]): Returns a tuple which contains the named subgroups of a match.
  • mo.start([group]): Returns the index for where a substring match starts.
  • mo.end([group]): Returns the index for where a substring match ends.
  • mo.span([group]): Returns the start and end points of a MatchObject in the form of a tuple.
  • mo.pos: The value of the position at which Python's regular expression engine began looking for a match. It reflects the start-position given to the search() or match() functions.
  • mo.endpos: The value of the position at which Python's regular expression engine ceased to look for a match. It reflects the end-position given to the search() or match() functions.
  • mo.lastindex: The numerical index of the last matched group.
  • mo.lastgroup: The name of the last group that was matched.
  • mo.re: The regular expression object used to produce the present MatchObject.
  • mo.string: The string passed to search() or match().
  1. About.com
  2. Technology
  3. Python
  4. Python Library
  5. Python Library - Python Regular Expressions - RegEx MatchObjects

©2014 About.com. All rights reserved.