Translating a Perl regex into Python

Stephan Tolksdorf andorxor at gmx.de
Fri Sep 7 07:13:56 EDT 2001


Hi,

I'm having a problem to translate a complex Perl regular expression which
I've found in the Perl faq. To be honest, I haven't tried to fully
understand it, but it seems to flawlessly strip all c++ like comments from
sourcefiles.

# The Perl regex from the Perl faq
content =~
s#/\*[^*]*\*+([^/*][^*]*\*+)*/|//[^\n]*|("(\\.|[^"\\])*"|'(\\.|[^'\\])*'|.[^
/"'\\]*)#$2#gs

# My attempt of a translation
rex =
re.compile("/\*[^*]*\*+([^/*][^*]*\*+)*/|//[^\n]*|(\"(\\.|[^\"\\])*\"|\'(\\.
|[^\'\\])*\'|.[^/\"\'\\]*)", re.M | re.S)
content = rex.sub('\2', content)

But I'm getting "sre_constants.error: unexpected end of regular
expression"...

Is there a kind soul who could help me?

Best Regards,
  Stephan





More information about the Python-list mailing list