By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,036 Members | 1,963 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,036 IT Pros & Developers. It's quick & easy.

regular expression

P: n/a
Hi all,

I wrote a little program. Here it is:

import sys
import Mk4py

db = Mk4py.storage("c:\\datafile.mk",1)
vw = db.view("people")

def func(row):
try:
if row.Nachname[0:1]=='G':
return 1
else:
return 0
except AttributeError:
return 0
vf = vw.filter(func)

for r in vf:
print vw[r.index].Nachname,vw[r.index].Kongressbereich
I create a database that contains a table. 'Nachname' and
'Kongressbereich' are special fieldnames. This program can search for
a special letter. In my example it is 'G'. and the search takes place
in 'Nachname'.
Mow I want to use regular expression. So that I can limit my search.
For example: I can search for Ge and it is not relevant wich letters
follow
Could somebody help me with this task?

Wiebke
Jul 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Wiebke Pätzold wrote:
On Thu, 31 Jul 2003 14:24:01 +0200, Thomas Güttler
<gu******@thomas-guettler.de> wrote:
Wiebke Pätzold wrote:

Please cut unimportant parts if you reply.
Can you tell me please on wich place in my program the if-statement
will be inserted?
At the same place where you have it (if ...=="G")
And then I have another question too. After the if statement there
must be a print statement. I have no idea how I must write my program
so that it is right and executably.
Can you write it me exactly how this program should look like?


You seem to be running on windows ("C:\..."). I think double-clicking
on it should execute it.

If you want to make it executable without python installed: Have a look
at py2exe.

Since you seem to be a german: Maybe my Python introduction helps you:
http://www.thomas-guettler.de/vortra...nfuehrung.html

thomas
Jul 18 '05 #2

P: n/a
On Thu, 31 Jul 2003 10:46:59 -0500, Jeff Epler <je****@unpythonic.net>
wrote:
These are all untested, because I don't have Mk4py or your datafile to
try it on.

I might write this:
import re
pattern = re.compile("^Ge")
def func(row):
try:
nachname = row.Nachname
except AttributeError:
return 0
return pattern.search(nachname) is not None

vf = vw.filter(func)

If you're using a Python version with nested scopes, you could use them
in this case:
import re
def make_func(pattern):
pattern = re.compile(pattern)
def func(row):
try:
nachname = row.Nachname
except AttributeError:
return 0
return pattern.search(nachname) is not None
return func

vf = vw.filter(make_func("^Ge"))

or you can make a callable filter object by using classes:
import re
class PatternFilter:
def __init__(self, pattern):
self.pattern = re.compile(pattern)

def __call__(self, row):
try:
nachname = row.Nachname
except AttributeError:
return 0
return self.pattern.search(Nachname) is not None
vf = vw.filter(PatternFilter("^Ge"))

Jeff


thank you for the part of the program.
But I have one question.
If I insert your first suggestion- there is print nothin. Is it
possible that there is missing something?

Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.