Regex not matching a string

Peter Otten __peter__ at web.de
Wed Jan 9 11:57:03 CET 2013


python.prog29 at 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 */ '





More information about the Python-list mailing list