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

T

TtfnJohn

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
 
P

Peter Otten

TtfnJohn said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top