regular expression

Harvey Thomas hst at empolis.co.uk
Thu Jul 31 11:53:44 EDT 2003


Wiebke Pätzold wrote:
> 
> 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
> -- 
import re at the top of the program and replace your func function with (untested):

compiled_pattern = re.compile('Ge') #match pattern is anything starting with 'Ge'
def func(row):
    try:
        if compiled_pattern.match(row.Nachname):
            return 1
        else:
            return 0
    except AttributeError:
        return 0

_____________________________________________________________________
This message has been checked for all known viruses by the MessageLabs Virus Scanning Service.





More information about the Python-list mailing list