Converting Perl Regex to Python!

Alex Martelli aleaxit at yahoo.com
Mon Dec 4 22:45:56 CET 2000


<thelocust at my-deja.com> wrote in message news:90h0a8$jgm$1 at nnrp1.deja.com...
> I'm working on porting a program from Perl to Python, and i'm to a
> point where I need to convert their Perl regex stuff over to python.
> Now, I understand what is going on in the Perl if statement below, but
> i haven't seen to many good examples of regex stuff for Python, so i'm
> hurting for an translation
>
> The string i need to grab the info from looks like this:
>
> MSG notify timecode [00:00:00:00]
>
> if ($msg =~ /^MSG notify timecode \[(\d\d:\d\d:\d\d):\d\d\]/) {
>     $cajun->{state_clock}=$1;
>     }
>
> So, how exactly do i convert this to python?

import re

mo = re.match(r'MSG notify timecode \[(\d\d:\d\d:\d\d):\d\d\]', msg)

if mo:
    cajun.state_clock = mo.group(1)


Alex







More information about the Python-list mailing list