Regex not matching a string

Steven D'Aprano steve+comp.lang.python at pearwood.info
Wed Jan 9 05:33:55 EST 2013


On Wed, 09 Jan 2013 02:08:23 -0800, python.prog29 wrote:

> Hi All -
> 
> 
> 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?

It works for me.

Some observations:

Perhaps you should consider using the glob module rather than manually 
using fnmatch. That's what glob does.

Also, you never actually write to the files, is that deliberate?

Finally, perhaps your regex simply doesn't match what you think it 
matches. Do you actually have any files containing the needle 

"/* ... This is a test comment ... */"

(where the ... are any characters) exactly as shown?

Instead of giving us all the irrelevant code that has nothing to do with 
matching a regex, you should come up with a simple piece of example code 
that demonstrates your problem. Or, in this case, *fails* to demonstrate 
the problem.

import re
haystack = "aaa\naaa /*xxxThis is a test comment \nxxx*/aaa\naaa\n"
needle = "This is a test comment"
pattern = re.compile(r'/\*.*?'+ needle + '.*?\*/', re.DOTALL)
print haystack
print re.sub(pattern, "", haystack)


-- 
Steven



More information about the Python-list mailing list