473,386 Members | 1,733 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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?

Jul 4 '08 #1
2 1781
On Jul 4, 12:33*pm, Marcin Krol <mrk...@gmail.comwrote:
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
Jul 4 '08 #2
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
Jul 4 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Michael Rockwell | last post by:
I am new to using C# generics and I am liking what I am finding. However the examples in online help are lacking. Can someone help me with the FindAll method of the generic List class? As I...
5
by: Dave | last post by:
Hi All C# ADSI samples that I run cause unspecified errors. If I translate them into VB.NET they run fine. Here's an example: public string getEmail(string LDAPPath, string username) {...
5
by: Mystilleef | last post by:
Hello, Is there a simple flag to set to allow overlapping matches for the findall() regular expression method? In other words, if a string contains five occurrences of the string pattern "cat",...
1
by: JerryB | last post by:
Hi, I have a string like this: invalidStr = "192.168.*.1" I want to be sure I don't get a * followed by a number, i.e. I want invalidStr to be invalid. So I do: numberAfterStar =...
3
by: DagoFlores | last post by:
Hi, let's take a look to this code that we already know.: List<stringdinosaurs = new List<string>(); dinosaurs.Add("Compsognathus"); dinosaurs.Add("Amargasaurus"); .... List<stringsublist =...
5
by: David Longnecker | last post by:
I'm working to create a base framework for our organization for web and client-side applications. The framework interfaces with several of our systems and provides the business and data layer...
7
by: Abhishek | last post by:
Hi I am using a generic list in my program to have a list of objects stored. I want to compare all the objects in this list with object of another class and find all the objects which meet the...
2
by: =?Utf-8?B?bWdvbnphbGVzMw==?= | last post by:
I have a List<tobject consisting of objects which in themselves consist of BindingListViews of objects. When I want to search for a object value I normally create a foreach loop and increment a...
1
by: Doug | last post by:
Hi, I have a collection of objects and I need to compare each item in the collection to see if it's a match on any or all of the others. I have stored my collection in an item like so: ...
2
by: Alexnb | last post by:
Okay, I am not sure if there is a better way of doing this than findAll() but that is how I am doing it right now. I am making an app that screen scapes dictionary.com for definitions. However, I...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.