473,799 Members | 3,025 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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,*!wt yd@*.dip.t-dialin.net:*!yl x@*.dip.t-
dialin.net:*!la jaz@*.dip.t-dialin.net::::: :'

reobj = re.compile(r"(. *),(.*),(.*)::: (.*):::(.*)")

testString1 = reobj.search(te stString)

if testString1:
match0 = testString1.gro up(0)
match1 = testString1.gro up(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.creat e(('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('friendsli st','r')

while 1:
line = voiceList.readl ine()
if not line:
break
print len(line)
line = line[:-2]
print line
print len(line)
regex = testString.sear ch(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.spl it(':')
counterSplit = len(doneSplit)

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

insertRecordLis t[0] = targetUser
insertRecordLis t[1] = targetName

for i in range(2,counter Split,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 1163
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,*!wt yd@*.dip.t-dialin.net:*!yl x@*.dip.t-
dialin.net:*!la jaz@*.dip.t-dialin.net::::: :'

reobj = re.compile(r"(. *),(.*),(.*)::: (.*):::(.*)")

testString1 = reobj.search(te stString)

if testString1:
match0 = testString1.gro up(0)
match1 = testString1.gro up(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.creat e(('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('friendsli st','r')

while 1:
line = voiceList.readl ine()
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
2418
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. zzz (empty) 4. www g=h,i=j,l=m
9
4592
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
2168
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 RegEx.IsMatch but it behaves quite differently. Is there a way I can mimic the DOS filtering of filenames (eg. "*.*" or "*" returns all files, "*.xls" returns all excel files, "workbook*" returns all files begining with "workbook" etc)? thanks in...
7
2620
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) every occurrence of characters "p", "q", "r", "s" with "y". Right now, I do it as follows:
3
8261
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, "{\&|\'}", "{&|'}") Thanks, Craig
5
3058
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 quotes or double quotes can be used and there can be any number of spaces or returns between any element. Keywords can contain special characters except for a comma or a closed bracket. For example, the HTML might be:
55
6253
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 C# in some way? Or maybe no, because it is similar to a global variable (with its scope restricted) which C# is dead against? Zytan
16
2255
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) pattern search functions. Where performance is an issue you can alway write your own specialized routine of course. However, for the occasional pattern search where performance isn't an issue, would most seasoned .NET developers rely on "Regex" and...
15
6005
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 the Regex.Split function to split the string whenever the #Key(val) occurrs but that keeps the #Key(val) occurrences to that I can reconstruct the final string after doing certain operations on each token (I need to basically convert each string...
0
9546
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10268
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10031
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6809
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5467
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5593
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4146
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3762
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2941
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.