Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

Bug in re.findall?

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


mrkafk at gmail

Jul 4, 2008, 3:33 AM

Post #1 of 4 (86 views)
Permalink
Bug in re.findall?

Hello everyone,

Is there a bug in re.findall in Python 2.4? See:

subnetlist="192.168.100.0 , 192.168.101.0"
ipre=re.compile("([0-9]{1,3}\.){3}[0-9]{1,3}")

>>> ipre.findall(subnetlist)

['100.', '101.']


But:

a=ipre.finditer(subnetlist)

>>> a.next().group()
'192.168.100.0'
>>> a.next().group()
'192.168.101.0'
>>> a.next().group()
Traceback (most recent call last):
File "<stdin>", line 1, in ?
StopIteration

Also:

>>> ipre.search(subnetlist).group()
'192.168.100.0'

Is this a bug or am I doing smth wrong?

--
http://mail.python.org/mailman/listinfo/python-list


richie at entrian

Jul 4, 2008, 4:14 AM

Post #2 of 4 (77 views)
Permalink
Re: Bug in re.findall? [In reply to]

Hi Marcin,

> subnetlist="192.168.100.0 , 192.168.101.0"
> ipre=re.compile("([0-9]{1,3}\.){3}[0-9]{1,3}")
>
> >>> ipre.findall(subnetlist)
> ['100.', '101.']

Correct - it returns the most recently captured text for your sole group.

> a=ipre.finditer(subnetlist)
> >>> a.next().group()
> '192.168.100.0'

Also correct, because match.group() returns the whole of the matched text.
If you wanted just your captured piece, you need this:

> >>> a.next().group(1)
> '100.'

Hope that helps!

--
Richie Hindle
richie [at] entrian
http://entrian.com
--
http://mail.python.org/mailman/listinfo/python-list


dwahli at gmail

Jul 4, 2008, 4:21 AM

Post #3 of 4 (72 views)
Permalink
Re: Bug in re.findall? [In reply to]

On Jul 4, 12:33 pm, Marcin Krol <mrk...@gmail.com> wrote:
> Hello everyone,
>
> Is there a bug in re.findall in Python 2.4? See:
>
> subnetlist="192.168.100.0 , 192.168.101.0"
> ipre=re.compile("([0-9]{1,3}\.){3}[0-9]{1,3}")
>
>  >>> ipre.findall(subnetlist)
>
> ['100.', '101.']
>
> But:
>
> a=ipre.finditer(subnetlist)
>
>  >>> a.next().group()
> '192.168.100.0'
>  >>> a.next().group()
> '192.168.101.0'
>  >>> a.next().group()
> Traceback (most recent call last):
>    File "<stdin>", line 1, in ?
> StopIteration
>
> Also:
>
>  >>> ipre.search(subnetlist).group()
> '192.168.100.0'
>
> Is this a bug or am I doing smth wrong?

Look strange but match the Python documentation for re.findall:
"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"

you must use this RE for your example to match what you expect:
ipre=re.compile("(?:[0-9]{1,3}\.){3}[0-9]{1,3}")

but using re.finditer is IMHO better.

Cheer,
Dom
--
http://mail.python.org/mailman/listinfo/python-list


__peter__ at web

Jul 4, 2008, 4:24 AM

Post #4 of 4 (77 views)
Permalink
Re: Bug in re.findall? [In reply to]

Marcin Krol wrote:

> Hello everyone,
>
> Is there a bug in re.findall in Python 2.4? See:
>
> subnetlist="192.168.100.0 , 192.168.101.0"
> ipre=re.compile("([0-9]{1,3}\.){3}[0-9]{1,3}")
>
> >>> ipre.findall(subnetlist)
>
> ['100.', '101.']
>
>
> But:
>
> a=ipre.finditer(subnetlist)
>
> >>> a.next().group()
> '192.168.100.0'
> >>> a.next().group()
> '192.168.101.0'
> >>> a.next().group()
> Traceback (most recent call last):
> File "<stdin>", line 1, in ?
> StopIteration
>
> Also:
>
> >>> ipre.search(subnetlist).group()
> '192.168.100.0'
>
> Is this a bug or am I doing smth wrong?

>From the doc:

"""
findall( pattern, string[, flags])
Return a list of all non-overlapping matches of pattern in 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.
"""

So findall()'s behaviour changes depending on the number of explicit groups

None:

[m.group() for m in re.finditer(...)]

One:

[m.group(1) for m in re.finditer(...)]

More than one:

[m.groups() for m in re.finditer(...)]

all in accordance with the documentation.

Peter
--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.