Match Object Methods and Properties

The following sections describe the methods and properties of the match object.

group()

<match object>.group([name|num][,name|num]...)

The group() method gets matches of each group of the expression, or all matches if no group is specified. If a single argument is passed, the return value is a string containing the group specified. If no arguments are passed, the return value is a string containing the entire match. If a group is in part of a pattern that doesn't match, the return value is None. If it's in part of the pattern that matches multiple times, the last match is returned.

 >>> mo = search(r'(?P<first>t)(?P<last>est)',"test") >>> mo.group() 'test' >>> mo.group("first") 't' >>> mo.group("last") 'est' ...

Get Python Programming with the Java™ Class Libraries: A Tutorial for Building Web and Enterprise Applications with Jython now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.