My first Python program AND IT DOESN'T WORK. Would someone please explain why?
aleaxit at yahoo.com
Tue Feb 18 10:09:15 CET 2003
> I want to copy some parts of a web page to the windows clipboard and
> then modify from the clipboard two types of lines ( to start with )
> using regular expressions and finally put the revision in the
> clipboard to then save to a file. The following are the two lines of
> my program that puzzle me. The first line works as I expect, the
> second doesn't:
> d = re.sub(r'.*Reply to This.*','____________',d)
> d = re.sub(r'^Re:.*M $','',d)
$ in a RE matches on at the end of the whole string, UNLESS you
specify multiline mode, in which case it does also match just
before each newline. You did not specify multiline mode here.
Easiest and clearest way to do so may be the following:
nores = re.compile(r'^Re:.*M $', re.MULTILINE)
d = nores.sub('', d)
Of course, you need to compile the RE into nores only once,
if you run this in a loop -- but that's another issue. You
can also specify flags for multiline inside the RE's pattern
string, but that's complicated and not very readable, so I
suggest you avoid that.
More information about the Python-list