Why does one regex routine work and not the other one?

TtfnJohn john_wilson at telus.net
Mon Jun 11 02:10:15 EDT 2007


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
    print len(line)
    line = line[:-2]
    print line
    print len(line)
    regex = testString.search(line)
    # fails here with regex showing a value of None in the debugger in
Komodo
    if regex:
        targetUser = regex.group(1)
        targetFlag = regex.group(2)
        targetHosts = regex.group(3)
        targetName = regex.group(4)
    retVal = targetFlag.find('v')
    if retVal == -1:
        continue
    doneSplit = targetHosts.split(':')
    counterSplit = len(doneSplit)

    # initialize or refresh list for database insertion
    insertRecordList = []
    insertRecordList = insertRecordList * 9

    insertRecordList[0] = targetUser
    insertRecordList[1] = targetName

    for i in range(2,counterSplit,1):

Obviously I don't get down to the part where I start to populate a
database record and it does look a bit kludgey to this Python n00b.

My question is that if it is bringing in strings from the file with
the same format as the one in the first listing why would it all fail
here?  Could the newline character at the end of the line be the
villian of the piece?

Thanks for any advice in advance

John




More information about the Python-list mailing list