Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #74041 > unrolled thread

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

Started byrxjwg98@gmail.com
First post2014-07-06 11:26 -0700
Last post2014-07-06 20:13 +0100
Articles 2 — 2 participants

Back to article view | Back to comp.lang.python


Contents

  What is the difference between matchObj.group() and matchObj.group(0) rxjwg98@gmail.com - 2014-07-06 11:26 -0700
    Re: What is the difference between matchObj.group() and matchObj.group(0) MRAB <python@mrabarnett.plus.com> - 2014-07-06 20:13 +0100

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

Fromrxjwg98@gmail.com
Date2014-07-06 11:26 -0700
SubjectWhat is the difference between matchObj.group() and matchObj.group(0)
Message-ID<809dbbdf-b452-4f25-a1ff-f00a2202939d@googlegroups.com>
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!!"

[toc] | [next] | [standalone]


#74048

FromMRAB <python@mrabarnett.plus.com>
Date2014-07-06 20:13 +0100
Message-ID<mailman.11554.1404674030.18130.python-list@python.org>
In reply to#74041
On 2014-07-06 19:26, rxjwg98@gmail.com wrote:
> 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?
>
matchObj.group(g) returns what was captured by group g.

If you don't specify the group, it'll assume that you want group 0,
which is the entire part of the string that matched.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web