regular expression

Thomas Güttler guettler at thomas-guettler.de
Thu Jul 31 08:24:01 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?

You can do it without regular expressions, too:


if nachname.startswith("Ge"): ...


# "Ge" somewhere in the string?
if nachname.find("Ge")!=-1: ...

The corresponding regular expressions would be:

if re.match(r'^Ge.*$'): ...

if re.match(r'^.*Ge.*$'): ...


.* matches everything
^ matches start of string
$ matches end of string

See the documentation of the module "re"


 thomas






More information about the Python-list mailing list