Why does one regex routine work and not the other one?
Peter Otten
__peter__ at web.de
Mon Jun 11 03:17:41 EDT 2007
TtfnJohn wrote:
> I have two small scripts that while on the surface should both work
> the problem is they don't.
>
> Here's the first one:
> import re
>
> testString = 'Thap,fpvi,*!wtyd@*.dip.t-dialin.net:*!ylx@*.dip.t-
> dialin.net:*!lajaz@*.dip.t-dialin.net::::::'
>
> reobj = re.compile(r"(.*),(.*),(.*):::(.*):::(.*)")
>
> testString1 = reobj.search(testString)
>
> if testString1:
> match0 = testString1.group(0)
> match1 = testString1.group(1)
>
> This works as expected with any number of seed strings.
>
> Now then:
>
> This one consistently fails even though it should work, as near as I
> can tell.
> import os
> import re
> import readline
> from buzhug import Base
>
> # initialize a few things
> voiceuser = Base('voiceuser')
> # now to create & open the database. If the database already exists
> this will
> # simply open it
> voiceuser.create(('name',str),('ircname',str),('first',str),
> ('second',str),('third',str),('fourth',str),('fifth',str),
> ('sixth',str),('seventh',str),mode="open")
>
> #next is to open the file we'll read from and then process it and add
> the names
> # to the database
> # the first step is to compile the regular expression
> testString = re.compile(r"(.*),(.*),(.*):::(.*):::(.*)")
> voiceList = open('friendslist','r')
>
> while 1:
> line = voiceList.readline()
> if not line:
> break
The above is spelt
for line in voiceList:
> print len(line)
> line = line[:-2]
Change that to
line = line[:-1]
or, more robust,
line = line.rstrip()
Otherwise you might be clipping the last ":".
Peter
More information about the Python-list
mailing list