F: How can I make re.sub() replace patterns across newlines

Hans Nowak hans at zephyrfalcon.org
Sun Feb 1 20:31:42 EST 2004


Viktor Rosenfeld wrote:
> Hi,
> 
> I want to strip a JAVA file of /* */ like comments.  Unfortunately, the
> simple regexp "\/\*.*\*\/" only works on comments, that are on one line. 
> Is there a simple way to remove comments that go across several lines with
> python regexp's?  I tried re.M to no avail.

Something like:

import re
pattern = re.compile("/\*.*?\*/", re.MULTILINE|re.DOTALL)
stripped_data = pattern.sub("", data)

Note that I added a ? to the regex, so it won't be "greedy".

HTH,

-- 
Hans (hans at zephyrfalcon.org)
http://zephyrfalcon.org/






More information about the Python-list mailing list