forked from sbditto85/pythonregex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregex.py
133 lines (114 loc) · 4.17 KB
/
regex.py
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
import re
from Tkinter import *
def command(f,*args,**kwargs):
return lambda: f(*args,**kwargs)
root = Tk()
root.title("Regex Playin' Around")
top = Frame(master=root)
filtertext = Text(master=top,width=120,height=20)
filtertext.pack(side=LEFT)
middle = Frame(master=root)
regextext = Text(master=middle,width=123,height=1)
regextext.pack(side=LEFT,fill=X)
bottom = Frame(master=root)
resulttext = Text(master=bottom,width=120,height=20)
resulttext.pack(side=LEFT,fill=X)
#txtarea.config(state=DISABLED)
#functions
def findall():
s = filtertext.get(1.0,END)[:-1]
rs = regextext.get(1.0,END)[:-1]
if s and rs:
r = re.compile(rs)
resulttext.delete(1.0,END)
resulttext.insert(INSERT,r.findall(s))
def search():
s = filtertext.get(1.0,END)[:-1]
rs = regextext.get(1.0,END)[:-1]
if s and rs:
r = re.compile(rs)
resulttext.delete(1.0,END)
found = r.search(s)
if found:
start, end = found.start(), found.end()
resulttext.insert(INSERT," ".join(["start:",str(start),"\n"]))
resulttext.insert(INSERT," ".join(["stop:",str(end),"\n"]))
resulttext.insert(INSERT," ".join(["subtring:",s[start:end],"\n"]))
resulttext.insert(INSERT," ".join(["groups:",str(found.groups()),"\n"]))
def match():
s = filtertext.get(1.0,END)[:-1]
rs = regextext.get(1.0,END)[:-1]
if s and rs:
r = re.compile(rs)
resulttext.delete(1.0,END)
found = r.match(s)
if found:
start, end = found.start(), found.end()
resulttext.insert(INSERT," ".join(["start:",str(start),"\n"]))
resulttext.insert(INSERT," ".join(["stop:",str(end),"\n"]))
resulttext.insert(INSERT," ".join(["subtring:",s[start:end],"\n"]))
resulttext.insert(INSERT," ".join(["groups:",str(found.groups()),"\n"]))
def sub():
s = filtertext.get(1.0,END)[:-1]
rs = regextext.get(1.0,END)[:-1]
st = subtext.get(1.0,END)[:-1]
if not st: st = ""
if s and rs:
r = re.compile(rs)
resulttext.delete(1.0,END)
try:
resulttext.insert(INSERT,r.sub(st,s))
except:
resulttext.insert(INSERT,"DOH! had an error")
def split():
s = filtertext.get(1.0,END)[:-1]
rs = regextext.get(1.0,END)[:-1]
if s and rs:
r = re.compile(rs)
resulttext.delete(1.0,END)
resulttext.insert(INSERT,r.split(s))
# Add buttons to toolbar
toolbar = Frame(master=root)
findallbutton = Button(master=toolbar, text="Findall", command=findall)
findallbutton.pack(side=LEFT)
searchbutton = Button(master=toolbar, text="Search", command=search)
searchbutton.pack(side=LEFT)
matchbutton = Button(master=toolbar, text="Match", command=match)
matchbutton.pack(side=LEFT)
splitbutton = Button(master=toolbar, text="Split", command=split)
splitbutton.pack(side=LEFT)
label = Label(master=toolbar, text="Sub String:")
label.pack(side=LEFT)
subtext = Text(master=toolbar,width=50,height=1)
subtext.pack(side=LEFT)
subbutton = Button(master=toolbar, text="Sub", command=sub)
subbutton.pack(side=LEFT)
quitbutton = Button(master=toolbar, text="Quit", command=root.quit)
quitbutton.pack(side=LEFT)
filterheader = Frame(master=root)
label = Label(master=filterheader, text="Original String:")
label.pack(side=LEFT)
filterheader.pack(fill=X)
top.pack()
regexheader = Frame(master=root)
label = Label(master=regexheader, text="Regex String:")
label.pack(side=LEFT)
regexheader.pack(fill=X)
middle.pack()
toolbar.pack(fill=X)
resultheader = Frame(master=root)
label = Label(master=resultheader, text="Result String:")
label.pack(side=LEFT)
resultheader.pack(fill=X)
bottom.pack()
# Make the Text area scrollable (scrollable widget (Listbox, Text, Canvas, Entry) and its scrollbar must have same parent)
xrscrollbar = Scrollbar(master=bottom)
xrscrollbar.pack(side=RIGHT,fill=Y)
resulttext.config(yscrollcommand=xrscrollbar.set)
xrscrollbar.config(command=resulttext.yview)
xfscrollbar = Scrollbar(master=top)
xfscrollbar.pack(side=RIGHT, fill=Y)
filtertext.config(yscrollcommand=xfscrollbar.set)
xfscrollbar.config(command=filtertext.yview)
if __name__ == "__main__":
root.mainloop()