[Tutor] re.findall parentheses problem

Michael Scharf mnshtb at gmail.com
Tue Sep 14 19:09:21 CEST 2010


Hi Evert,

Thank you.  I should have figured "groups" were the paren groups.  I see it
clearly now.  And your solution will work for the larger thing I'm trying to
do --- thanks.

And yes: I know this matches some non-date-like dates, but the data is such
that it should work out ok.

Thanks again,
Mike



> This will also match '1 Janry 2010'.
> Not sure if it should?
>
>
> > A friend says: " I think that the problem is that every time that you
> have a parenthesis you get an output. Maybe there is a way to suppress
> this."
> >
>
The docs say: " If one or more groups are present in the pattern, return a
> list of groups". So your friend is right.
>
> In fact, your last example shows exactly this: it shows a list of two
> tuples. The tuples contain individual group matches, the two list elements
> are your two date matches.
> You could solve this by grouping the entire regex (so r"(([0-3 ....
> [0-9]))" ; I would even use a named group), and then picking out the first
> tuple element of each list element:
> [(' January 1, 2008', '', ' ', ' ', '', 'January', 'January', '', '', '',
> '', '', '', '', '', '', '', '', ' ', ' ', '', '1,', ' ', ' ', '', '2008'),
> (' January 2, 2008', '', ' ', ' ', '', 'January', 'January', '', '', '', '',
> '', '', '', '', '', '', '', ' ', ' ', '', '2,', ' ', ' ', '', '2008')]
>
>
> Hth,
>
>  Evert
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100914/2a552936/attachment.html>


More information about the Tutor mailing list