473,387 Members | 1,942 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,387 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 1148
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Alan Pretre | last post by:
Can anyone help me figure out a regex pattern for the following input example: xxx:a=b,c=d,yyy:e=f,zzz:www:g=h,i=j,l=m I would want four matches from this: 1. xxx a=b,c=d 2. yyy e=f 3....
9
by: Tim Conner | last post by:
Is there a way to write a faster function ? public static bool IsNumber( char Value ) { if (Regex.IsMatch( Value.ToString(), @"^+$" )) { return true; } else return false; }
16
by: Andrew Baker | last post by:
I am trying to write a function which provides my users with a file filter. The filter used to work just using the VB "Like" comparision, but I can't find the equivilant in C#. I looked at...
7
by: bill tie | last post by:
I'd appreciate it if you could advise. 1. How do I replace "\" (backslash) with anything? 2. Suppose I want to replace (a) every occurrence of characters "a", "b", "c", "d" with "x", (b)...
3
by: Craig Buchanan | last post by:
Is there a way to combine these two Replace into a single line? Regex.Replace(Subject, "\&", "&") Regex.Replace(Subject, "\'", "'") Perhaps Regex.Replace(Subject, "{\&|\'}", "{&|'}")...
5
by: Digital.Rebel.18 | last post by:
I'm trying to figure out how to extract the keywords from an HTML document. The input string would typically look like: <meta name='keywords' content='word1, more stuff, etc'> Either single...
55
by: Zytan | last post by:
I see that static is more restricted in C# than in C++. It appears usable only on classes and methods, and data members, but cannot be created within a method itself. Surely this is possible in...
16
by: Mark Chambers | last post by:
Hi there, I'm seeking opinions on the use of regular expression searching. Is there general consensus on whether it's now a best practice to rely on this rather than rolling your own (string)...
15
by: nagar | last post by:
I need to split a string whenever a separator string is present (lets sey #Key(val) where val is a variable) and rejoin it in the proper order after doing some processing. Is there a way to use...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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.