1 | n/a | #!/usr/bin/env python3 |
---|
2 | n/a | |
---|
3 | n/a | """Basic regular expression demonstration facility (Perl style syntax).""" |
---|
4 | n/a | |
---|
5 | n/a | from tkinter import * |
---|
6 | n/a | import re |
---|
7 | n/a | |
---|
8 | n/a | class ReDemo: |
---|
9 | n/a | |
---|
10 | n/a | def __init__(self, master): |
---|
11 | n/a | self.master = master |
---|
12 | n/a | |
---|
13 | n/a | self.promptdisplay = Label(self.master, anchor=W, |
---|
14 | n/a | text="Enter a Perl-style regular expression:") |
---|
15 | n/a | self.promptdisplay.pack(side=TOP, fill=X) |
---|
16 | n/a | |
---|
17 | n/a | self.regexdisplay = Entry(self.master) |
---|
18 | n/a | self.regexdisplay.pack(fill=X) |
---|
19 | n/a | self.regexdisplay.focus_set() |
---|
20 | n/a | |
---|
21 | n/a | self.addoptions() |
---|
22 | n/a | |
---|
23 | n/a | self.statusdisplay = Label(self.master, text="", anchor=W) |
---|
24 | n/a | self.statusdisplay.pack(side=TOP, fill=X) |
---|
25 | n/a | |
---|
26 | n/a | self.labeldisplay = Label(self.master, anchor=W, |
---|
27 | n/a | text="Enter a string to search:") |
---|
28 | n/a | self.labeldisplay.pack(fill=X) |
---|
29 | n/a | self.labeldisplay.pack(fill=X) |
---|
30 | n/a | |
---|
31 | n/a | self.showframe = Frame(master) |
---|
32 | n/a | self.showframe.pack(fill=X, anchor=W) |
---|
33 | n/a | |
---|
34 | n/a | self.showvar = StringVar(master) |
---|
35 | n/a | self.showvar.set("first") |
---|
36 | n/a | |
---|
37 | n/a | self.showfirstradio = Radiobutton(self.showframe, |
---|
38 | n/a | text="Highlight first match", |
---|
39 | n/a | variable=self.showvar, |
---|
40 | n/a | value="first", |
---|
41 | n/a | command=self.recompile) |
---|
42 | n/a | self.showfirstradio.pack(side=LEFT) |
---|
43 | n/a | |
---|
44 | n/a | self.showallradio = Radiobutton(self.showframe, |
---|
45 | n/a | text="Highlight all matches", |
---|
46 | n/a | variable=self.showvar, |
---|
47 | n/a | value="all", |
---|
48 | n/a | command=self.recompile) |
---|
49 | n/a | self.showallradio.pack(side=LEFT) |
---|
50 | n/a | |
---|
51 | n/a | self.stringdisplay = Text(self.master, width=60, height=4) |
---|
52 | n/a | self.stringdisplay.pack(fill=BOTH, expand=1) |
---|
53 | n/a | self.stringdisplay.tag_configure("hit", background="yellow") |
---|
54 | n/a | |
---|
55 | n/a | self.grouplabel = Label(self.master, text="Groups:", anchor=W) |
---|
56 | n/a | self.grouplabel.pack(fill=X) |
---|
57 | n/a | |
---|
58 | n/a | self.grouplist = Listbox(self.master) |
---|
59 | n/a | self.grouplist.pack(expand=1, fill=BOTH) |
---|
60 | n/a | |
---|
61 | n/a | self.regexdisplay.bind('<Key>', self.recompile) |
---|
62 | n/a | self.stringdisplay.bind('<Key>', self.reevaluate) |
---|
63 | n/a | |
---|
64 | n/a | self.compiled = None |
---|
65 | n/a | self.recompile() |
---|
66 | n/a | |
---|
67 | n/a | btags = self.regexdisplay.bindtags() |
---|
68 | n/a | self.regexdisplay.bindtags(btags[1:] + btags[:1]) |
---|
69 | n/a | |
---|
70 | n/a | btags = self.stringdisplay.bindtags() |
---|
71 | n/a | self.stringdisplay.bindtags(btags[1:] + btags[:1]) |
---|
72 | n/a | |
---|
73 | n/a | def addoptions(self): |
---|
74 | n/a | self.frames = [] |
---|
75 | n/a | self.boxes = [] |
---|
76 | n/a | self.vars = [] |
---|
77 | n/a | for name in ('IGNORECASE', |
---|
78 | n/a | 'LOCALE', |
---|
79 | n/a | 'MULTILINE', |
---|
80 | n/a | 'DOTALL', |
---|
81 | n/a | 'VERBOSE'): |
---|
82 | n/a | if len(self.boxes) % 3 == 0: |
---|
83 | n/a | frame = Frame(self.master) |
---|
84 | n/a | frame.pack(fill=X) |
---|
85 | n/a | self.frames.append(frame) |
---|
86 | n/a | val = getattr(re, name) |
---|
87 | n/a | var = IntVar() |
---|
88 | n/a | box = Checkbutton(frame, |
---|
89 | n/a | variable=var, text=name, |
---|
90 | n/a | offvalue=0, onvalue=val, |
---|
91 | n/a | command=self.recompile) |
---|
92 | n/a | box.pack(side=LEFT) |
---|
93 | n/a | self.boxes.append(box) |
---|
94 | n/a | self.vars.append(var) |
---|
95 | n/a | |
---|
96 | n/a | def getflags(self): |
---|
97 | n/a | flags = 0 |
---|
98 | n/a | for var in self.vars: |
---|
99 | n/a | flags = flags | var.get() |
---|
100 | n/a | flags = flags |
---|
101 | n/a | return flags |
---|
102 | n/a | |
---|
103 | n/a | def recompile(self, event=None): |
---|
104 | n/a | try: |
---|
105 | n/a | self.compiled = re.compile(self.regexdisplay.get(), |
---|
106 | n/a | self.getflags()) |
---|
107 | n/a | bg = self.promptdisplay['background'] |
---|
108 | n/a | self.statusdisplay.config(text="", background=bg) |
---|
109 | n/a | except re.error as msg: |
---|
110 | n/a | self.compiled = None |
---|
111 | n/a | self.statusdisplay.config( |
---|
112 | n/a | text="re.error: %s" % str(msg), |
---|
113 | n/a | background="red") |
---|
114 | n/a | self.reevaluate() |
---|
115 | n/a | |
---|
116 | n/a | def reevaluate(self, event=None): |
---|
117 | n/a | try: |
---|
118 | n/a | self.stringdisplay.tag_remove("hit", "1.0", END) |
---|
119 | n/a | except TclError: |
---|
120 | n/a | pass |
---|
121 | n/a | try: |
---|
122 | n/a | self.stringdisplay.tag_remove("hit0", "1.0", END) |
---|
123 | n/a | except TclError: |
---|
124 | n/a | pass |
---|
125 | n/a | self.grouplist.delete(0, END) |
---|
126 | n/a | if not self.compiled: |
---|
127 | n/a | return |
---|
128 | n/a | self.stringdisplay.tag_configure("hit", background="yellow") |
---|
129 | n/a | self.stringdisplay.tag_configure("hit0", background="orange") |
---|
130 | n/a | text = self.stringdisplay.get("1.0", END) |
---|
131 | n/a | last = 0 |
---|
132 | n/a | nmatches = 0 |
---|
133 | n/a | while last <= len(text): |
---|
134 | n/a | m = self.compiled.search(text, last) |
---|
135 | n/a | if m is None: |
---|
136 | n/a | break |
---|
137 | n/a | first, last = m.span() |
---|
138 | n/a | if last == first: |
---|
139 | n/a | last = first+1 |
---|
140 | n/a | tag = "hit0" |
---|
141 | n/a | else: |
---|
142 | n/a | tag = "hit" |
---|
143 | n/a | pfirst = "1.0 + %d chars" % first |
---|
144 | n/a | plast = "1.0 + %d chars" % last |
---|
145 | n/a | self.stringdisplay.tag_add(tag, pfirst, plast) |
---|
146 | n/a | if nmatches == 0: |
---|
147 | n/a | self.stringdisplay.yview_pickplace(pfirst) |
---|
148 | n/a | groups = list(m.groups()) |
---|
149 | n/a | groups.insert(0, m.group()) |
---|
150 | n/a | for i in range(len(groups)): |
---|
151 | n/a | g = "%2d: %r" % (i, groups[i]) |
---|
152 | n/a | self.grouplist.insert(END, g) |
---|
153 | n/a | nmatches = nmatches + 1 |
---|
154 | n/a | if self.showvar.get() == "first": |
---|
155 | n/a | break |
---|
156 | n/a | |
---|
157 | n/a | if nmatches == 0: |
---|
158 | n/a | self.statusdisplay.config(text="(no match)", |
---|
159 | n/a | background="yellow") |
---|
160 | n/a | else: |
---|
161 | n/a | self.statusdisplay.config(text="") |
---|
162 | n/a | |
---|
163 | n/a | |
---|
164 | n/a | # Main function, run when invoked as a stand-alone Python program. |
---|
165 | n/a | |
---|
166 | n/a | def main(): |
---|
167 | n/a | root = Tk() |
---|
168 | n/a | demo = ReDemo(root) |
---|
169 | n/a | root.protocol('WM_DELETE_WINDOW', root.quit) |
---|
170 | n/a | root.mainloop() |
---|
171 | n/a | |
---|
172 | n/a | if __name__ == '__main__': |
---|
173 | n/a | main() |
---|