Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > regular expression

Reply
Thread Tools

regular expression

 
 
Wiebke Pätzold
Guest
Posts: n/a
 
      07-31-2003
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
 
Reply With Quote
 
 
 
 
Thomas =?ISO-8859-15?Q?G=FCttler?=
Guest
Posts: n/a
 
      07-31-2003
Wiebke Pätzold wrote:

> On Thu, 31 Jul 2003 14:24:01 +0200, Thomas Güttler
> <(E-Mail Removed)> 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


 
Reply With Quote
 
 
 
 
Wiebke Pätzold
Guest
Posts: n/a
 
      08-01-2003
On Thu, 31 Jul 2003 10:46:59 -0500, Jeff Epler <(E-Mail Removed)>
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?

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Seek xpath expression where an attribute name is a regular expression GIMME XML 3 12-29-2008 03:11 PM
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" Adem C++ 42 11-04-2008 12:39 PM
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" Adem C Programming 45 11-04-2008 12:39 PM
Matching abitrary expression in a regular expression =?iso-8859-1?B?bW9vcJk=?= Java 8 12-02-2005 12:51 AM
Dynamically changing the regular expression of Regular Expression validator VSK ASP .Net 2 08-24-2003 02:47 PM



Advertisments