Simple Python Clipboard RegEX Parser

Here is some Python code which will manipulate data copied to your clipboard. Just CRTL-C your data, run this Python script with your REGEX manipulators and paste back to the target. I leveraged this concept from Tim Koopmans (thanks Tim), but did a python implementation rather than perl.

import win32clipboard, win32gui, win32con
import re, time, os 

def openclip():
	win32clipboard.OpenClipboard()
	
# matchreplace occurrence=ALL for all, occurrence = n for n times.
# matchreplace occurrence=DOTALL will match across multiple lines 
def matchreplace(match, replace, occurrence):
	openclip()
	if occurrence == "ALL":
	   clipstuff=re.sub(match, replace, win32clipboard.GetClipboardData())
	elif occurrence == "DOTALL":
	   clipstuff=re.sub(match, replace, win32clipboard.GetClipboardData(), flags=re.DOTALL)
	else:
	   clipstuff=re.sub(match, replace, win32clipboard.GetClipboardData(),occurrence)
	closeclip(clipstuff)

def closeclip(clipboard):
	win32clipboard.EmptyClipboard()
	win32clipboard.SetClipboardText(clipboard)
	win32clipboard.CloseClipboard()	
	
def startparse():
	# Update top of script with modified date and time
	localtime = time.asctime( time.localtime(time.time()) )
	msghdr ="{\n\n\t /* Modified by Python Automated LoadParser for "+localtime+" */\n"
	matchreplace("{.", msghdr,1)

def finishparse():
	os.system("cls");
	print("===========================================================")	
print("Parser completed. Please paste to target destination.!")	
	print("===========================================================")	


startparse();	

#Replace Environment Variables
matchreplace("www.xxx.xxx.xxx.au", "{pWebIP}","ALL");

#Remove blocks of code not required based on match
matchreplace("flex_rtmp_send(.*?);",				"//===>flex_rtmp_send removed by Parser for<===","DOTALL");

finishparse();