471,337 Members | 1,025 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

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

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),('fi rst',str),
('second',str),('third',str),('fourth',str),('fift h',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

Jun 11 '07 #1
1 1056
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),('fi rst',str),
('second',str),('third',str),('fourth',str),('fift h',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
Jun 11 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Alan Pretre | last post: by
9 posts views Thread by Tim Conner | last post: by
7 posts views Thread by bill tie | last post: by
5 posts views Thread by Digital.Rebel.18 | last post: by
16 posts views Thread by Mark Chambers | last post: by

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.