python.pro...@gmail.com wrote: > In the following code ,am trying to remove a multi line - comment that > contains "This is a test comment" for some reason the regex is not > matching.. can anyone provide inputs on why it is so?
> def find_and_remove(haystack, needle): > pattern = re.compile(r'/\*.*?'+ needle + '.*?\*/', re.DOTALL) > return re.sub(pattern, "", haystack) If a comment does not contain the needle "/\*.*?" extends over the end of that comment: >>> re.compile(r"/\*.*?xxx").search("/* xxx */").group() '/* xxx' >>> re.compile(r"/\*.*?xxx").search("/* yyy */ /* xxx */").group() '/* yyy */ /* xxx' One solution may be a substitution function: >>> def sub(match, needle="xxx"): ... s = match.group() ... if needle in s: ... return "" ... else: ... return s ... >>> re.compile(r"/\*.*?\*/").sub(sub, "/* yyy */ /* xxx */") '/* yyy */ ' -- http://mail.python.org/mailman/listinfo/python-list