Converting Perl Regex to Python!

Robert Roy rjroy at takingcontrol.com
Mon Dec 4 17:35:37 EST 2000


On Mon, 04 Dec 2000 20:54:37 GMT, thelocust at my-deja.com wrote:

>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?
>
>
>Sent via Deja.com http://www.deja.com/
>Before you buy.

Python regular expressions (re module) use Perl syntax so the
translation in this case is very straightforward. 

#
import re
str = 'MSG notify timecode [10:02:34:00]'

m =  re.match('^MSG notify timecode \[(\d\d:\d\d:\d\d):\d\d\]', str)
if m:
    print 'foundit', m.group(1)
else:
    print 'no such luck'






More information about the Python-list mailing list