[Tutor] Re.findall question

2012-06-26 Thread Alexander Quest
I'm a bit confused about extracting data using re.search or re.findall.

Say I have the following code: tuples =
re.findall(r'blahblah(\d+)yattayattayatta(\w+)moreblahblahblah(\w+)over',
text)

So I'm looking for that string in 'text', and I intend to extract the parts
which have parentheses around them. And it works: the variable tuples,
which I assigned to get the return of re.findall, returns a tuple list,
each 'element' therein being a tuple of 3 elements (which is what I wanted
since I had 3 sets of parentheses).

My question is how does Python know to return just the part in the
parentheses and not to return the blahblah and the yattayattayatta,
etc...? The 're.search' function returns the whole thing, and if I want
just the parentheses parts, I do tuples.group(1) or tuples.group(2) or
tuples.group(3), depending on which set of parentheses I want. Does the
re.findall command by default ignore anything outside of the parentheses
and only return the parentheses as a grouping withing one tuple (i.e., the
first element in tuples would be, as it is, a list comprised of 3
elements corresponding respectively to the 1st, 2nd, and 3rd parentheses)?
Thank you for reading.

-Alex
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Re.findall question

2012-06-26 Thread Alan Gauld

On 26/06/12 23:30, Alexander Quest wrote:


My question is how does Python know to return just the part in the
parentheses and not to return the blahblah and the yattayattayatta,
etc...?


If you want to know *how* Python does it you will have to read the 
module code (probably in C so download the source code)



... Does the
re.findall command by default ignore anything outside of the parentheses
and only return the parentheses as a grouping withing one tuple


The help() function returns:

--
findall(pattern, string, flags=0)
Return a list of all non-overlapping matches in the string.

If one or more groups are present in the pattern, return a
list of groups; this will be a list of tuples if the pattern
has more than one group.

Empty matches are included in the result.
--

So that's what its defined to do. How it does it is another matter.


--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor