What is the difference between matchObj.group() and matchObj.group(0)

rxjwg98 at gmail.com rxjwg98 at gmail.com
Sun Jul 6 20:26:14 CEST 2014


Hi,

I cannot get the difference between matchObj.group() and matchObj.group(0),
Although there definitions are obvious different. And group() mentions 'tuple'.
tuple means all the elements in line object?



Match Object Methods

Description

group(num=0) This method returns entire match (or specific subgroup num) 
groups()     This method returns all matching subgroups in a tuple 
             (empty if there weren't any) 



I run the following code. Even I add more words to line object, Both have the
same output.

Could you clarify this question?


Thanks again.





#!/usr/bin/python
import re

line = "Cats are smarter than dogs"

matchObj = re.match( r'(.*) are (.*?) .*', line, re.M|re.I)

if matchObj:
   print "matchObj.group() : ", matchObj.group()
   print "matchObj.group(1) : ", matchObj.group(1)
   print "matchObj.group(2) : ", matchObj.group(2)
else:
   print "No match!!"



More information about the Python-list mailing list