regex syntax

Daniel Dittmar daniel.dittmar at sap.corp
Tue Dec 7 07:59:45 EST 2004


Andreas Volz wrote:
> string = "bild.jpg"
> 
> ich möchte jetzt einfach wissen ob in dem string ein ".jpg" vorkommt
> oder nicht und dann eine Entscheidung treffen. Ich hab mir schon
> überlegt einfach die letzten viel Stellen des strings "per Hand" auf die
> Zeichenfolge zu vergleichen und so regex zu umgehen. Aber ich muss es
> irgendwann ja doch mal nutzen ;-)

1.) If someone else should read your code:

import os

def hasExt (filename, expectedExtension):
     actualExtension = os.path.splitext (filename) [1]
     return actualExtension == expectedExtension

if hasExt ("build.jpg", ".jpg"):
     ...

2.) Debugging regular expression:

Just remove meaningful chunks from the end of the expression until you 
have an expected match. Then add them again one by one, most problems 
will become quite obvious. And how do I wish there was a debug mode that 
would make that somewhat automatic.

Daniel



More information about the Python-list mailing list