embedding executable code in a regular expression in Python

Stefan Behnel stefan.behnel-n05pAM at web.de
Mon Jul 17 00:35:02 EDT 2006


Avi Kak wrote:
> Does regular expression processing in Python allow for executable
> code to be embedded inside a regular expression?
> 
> For example, in Perl the following two statements
> 
> $regex = qr/hello(?{print "saw hello\n"})mello(?{print "saw
> mello\n"})/;
> "jellohellomello"  =~  /$regex/;
> 
> will produce the output
> 
>   saw hello
>   saw mello
> 
> Is it possible to do the same in Python with any modules that come
> with the standard distribution, or with any other modules?

Just in case you were referring to security concerns: Sufficiently complex REs
can take ages to compile and run and eat tons of memory, so there are always
issues involved here.

Stefan



More information about the Python-list mailing list