Welcome, guest | Sign In | My Account | Store | Cart

Instant regular expressions. On each keystroke, all matches of the given text are highlighted, and the corresponding replacements are performed. Parts of the regular expression can be selected to make their scope appear in the original text.

Python, 140 lines
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/env python
"""
Finding (and Replacing) Nemo

Instant Regular Expressions
Created by Aristide Grange
"""
import Tkinter as tk
import sre, itertools

windowTitle = "Finding (and Replacing) Nemo"
initialFind = r"n(.*?)e(.*?)m(.*?)o"
initialRepl = r"M\1A\2K\3I"
initialText = """\
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
"""
images = {
	"FIND":"R0lGODlhMAAiAPcAMf/////37//35//n1v97Off///f/9/f37/fexvfOvfeEQvd7QvdrQvdrKfdaKfdSMfdSIe/v9+/v7+/v5+/n3u/e1u/Wxu/Gre+1lO+tnO+thO+Ua+97Y+97Oe97Me9rOe9rMe9jOe9jMe9jIe9aMefe5+fe3ufezuece+eEWudzQudaIedS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EbzTzCRp5CADmAAwj+ORGPBcgwAAHo9ABGCYtm0ChwFHShlRiXhmHlkAcCiOeUodqQw5W0oXLAiamy4MOkjOyAaqxUymApDCEAADs=",
}
colors = ["#FF7B39","#80F121"]
emphColors = ["#DAFC33","#F42548"]
fieldParams = {
	"height":3,
	"width":70,
	"font":("monaco",14),
	"highlightthickness":0,
	"borderwidth":0,
	"background":"white",
}
textParams = {
	"bg":"#F7E0D4",
	"fg":"#2321F1",
	"highlightthickness":0,
	"width":1,
	"height":10,
	"font":("verdana",16),
	"wrap":"word",
}


class Zone:
	def __init__(self, image, initialField, initialText):
		frm = tk.Frame(root)
		frm.config(background="white")
		self.image = tk.PhotoImage(format='gif',data=images[image.upper()])
		self.imageDimmed = tk.PhotoImage(format='gif',data=images[image])
		self.img = tk.Label(frm)
		self.img.config(borderwidth=0)
		self.img.pack(side = "left")
		self.fld = tk.Text(frm, **fieldParams)
		self.initScrollText(frm,self.fld,initialField)
		frm = tk.Frame(root)
		self.txt = tk.Text(frm, **textParams)
		self.initScrollText(frm,self.txt,initialText)
		for i in range(2):
			self.txt.tag_config(colors[i], background = colors[i])
			self.txt.tag_config("emph"+colors[i], foreground = emphColors[i])
	def initScrollText(self,frm,txt,contents):
		scl = tk.Scrollbar(frm)
		scl.config(command = txt.yview)
		scl.pack(side="right",fill="y")
		txt.pack(side = "left", expand=True, fill="x")
		txt.config(yscrollcommand = scl.set)
		txt.insert("1.0",contents)
		frm.pack(fill = "x")
		tk.Frame(height=2, bd=1, relief="ridge").pack(fill="x")
	def refresh(self):
		self.colorCycle = itertools.cycle(colors)
		try:
			self.substitute()
			self.img.config(image = self.image)
		except sre.error:
			self.img.config(image = self.imageDimmed)


class FindZone(Zone):
	def addTags(self,m):
		color = self.colorCycle.next()
		self.txt.tag_add(color,"1.0+%sc"%m.start(),"1.0+%sc"%m.end())
		try:
			self.txt.tag_add("emph"+color,"1.0+%sc"%m.start("emph"),"1.0+%sc"%m.end("emph"))
		except:
			pass
	def substitute(self,*args):
		for color in colors:
			self.txt.tag_remove(color,"1.0","end")
			self.txt.tag_remove("emph"+color,"1.0","end")
		self.rex = sre.compile("") # default value in case of misformed regexp
		self.rex = sre.compile(self.fld.get("1.0","end")[:-1],sre.MULTILINE)
		try:
			sre.compile("(?P<emph>%s)" % self.fld.get(tk.SEL_FIRST,tk.SEL_LAST))
			self.rexSel = sre.compile("%s(?P<emph>%s)%s" % (
				self.fld.get("1.0",tk.SEL_FIRST),
				self.fld.get(tk.SEL_FIRST,tk.SEL_LAST),
				self.fld.get(tk.SEL_LAST,"end")[:-1],
			),sre.MULTILINE)
		except:
			self.rexSel = self.rex
		self.rexSel.sub(self.addTags,self.txt.get("1.0","end"))


class ReplaceZone(Zone):
	def addTags(self,m):
		s = sz.rex.sub(self.repl,m.group())
		self.txt.delete("1.0+%sc"%(m.start()+self.diff),"1.0+%sc"%(m.end()+self.diff))
		self.txt.insert("1.0+%sc"%(m.start()+self.diff),s,self.colorCycle.next())
		self.diff += len(s) - (m.end() - m.start())
	def substitute(self):
		self.txt.delete("1.0","end")
		self.txt.insert("1.0",sz.txt.get("1.0","end")[:-1])
		self.diff = 0
		self.repl = rex0.sub(r"\\g<\1>",self.fld.get("1.0","end")[:-1])
		sz.rex.sub(self.addTags,sz.txt.get("1.0","end")[:-1])


def launchRefresh(_):
	sz.fld.after_idle(sz.refresh)
	rz.fld.after_idle(rz.refresh)


root = tk.Tk()
root.resizable(height=False,width=True)
root.title(windowTitle)
root.minsize(width=250,height=0)
sz = FindZone("find",initialFind,initialText)
sz.fld.bind("<Button-1>",launchRefresh)
sz.fld.bind("<ButtonRelease-1>",launchRefresh)
sz.fld.bind("<B1-Motion>",launchRefresh)
sz.rexSel = sre.compile("")
rz = ReplaceZone("repl",initialRepl,"")
rex0 = sre.compile(r"(?<!\\)\\([0-9]+)")
root.bind_all("<Key>",launchRefresh)
launchRefresh(None)
root.mainloop()

Do regular expressions need to be boring? I have written this little tool for my students to play with regexps. Along with a video projector, you can immediately demonstrate the validity or/and the effect of any construction they want to test. It's my first (and probably my last ;-) Tkinter script: feel free to improve it for your own needs!

(The GUI's metaphor is based on a funny anonymous pic I have found in googling "Finding Nemo".)