My first Python program AND IT DOESN'T WORK. Would someone please explain why?

Sean 'Shaleh' Perry shalehperry at attbi.com
Tue Feb 18 03:36:57 EST 2003


On Monday 17 February 2003 21:37, m wrote:
> 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)
>
>

Your regex says "from start of string find 'Re:' followed by some amount of 
text (maybe none), then the letter 'M', a space, and finally the end of the 
string.  The problem here is the string in d does not start at 'Re:'.  '^' 
means the beginning of a string, not a line.  Try:

d = re.sub(r'\nRe:.*[A|P]M \n', '\n', d) # match [A|P]M for safety

instead.





More information about the Python-list mailing list