Translating a Perl regex into Python

Stephan Tolksdorf andorxor at gmx.de
Sat Sep 8 08:08:15 EDT 2001


> Try just taking the original Perl regex, and just wrap r'''...''' around
> it. ie:

Thanks for the help. How could I forget to use raw strings?

But it still doesn't work for me...

This perl program
--
$str = "// one line of comment \nfunction () { /* another comment */ }";
$str =~
s#/\*[^*]*\*+([^/*][^*]*\*+)*/|//[^\n]*|("(\\.|[^"\\])*"|'(\\.|[^'\\])*'|.[^
/"'\\]*)#$2#gs;
print $str;
---
returns the correct output.

The python program
--
import re
rex =
re.compile(r'''/\*[^*]*\*+([^/*][^*]*\*+)*/|//[^\n]*|("(\\.|[^"\\])*"|'(\\.|
[^'\\])*'|.[^/"'\\]*)''', re.M | re.S)
str = re.sub(rex, r"\2", "// one line of comment \nfunction () { /* another
comment */ }")
print str
--
returns the error "sre_constants.error: empty group".

I'm stuck...

Greetings,
  Stephan





More information about the Python-list mailing list