Regular expression help
Stephen Boulet
stephen.boulet at motorola.com
Tue Apr 1 09:58:07 EST 2003
Stephen Boulet wrote:
> How do I use the sub function to replace my match with my match
> prepended by a newline?
>
> Thanks.
>
> -- Stephen
>
Thanks to all who answered. I learned stuff.
Here's the final script that formats text on the windows clipboard
(cleanDates.pyw):
import win32clipboard,sys,re
# Get current string from clipboard. If string empty, error out.
win32clipboard.OpenClipboard(0)
if win32clipboard.IsClipboardFormatAvailable(win32clipboard.CF_TEXT):
s = win32clipboard.GetClipboardData(win32clipboard.CF_TEXT)
else:
from tkMessageBox import showerror
showerror('Error!','No text on clipboard')
sys.exit()
# Fix current string
p = re.compile(r'\d+/\d+/\d+')
def mysub(x):
return '\n\n' + x.group(0)
s = p.sub(mysub, s)
# Paste string to clipboard
win32clipboard.OpenClipboard(0)
win32clipboard.EmptyClipboard()
win32clipboard.SetClipboardText(s.strip())
win32clipboard.CloseClipboard()
More information about the Python-list
mailing list