1 """Basic regular expression demostration facility (Perl style syntax)."""
8 def __init__(self
, master
):
11 self
.promptdisplay
= Label(self
.master
, anchor
=W
,
12 text
="Enter a Perl-style regular expression:")
13 self
.promptdisplay
.pack(side
=TOP
, fill
=X
)
15 self
.regexdisplay
= Entry(self
.master
)
16 self
.regexdisplay
.pack(fill
=X
)
17 self
.regexdisplay
.focus_set()
21 self
.statusdisplay
= Label(self
.master
, text
="", anchor
=W
)
22 self
.statusdisplay
.pack(side
=TOP
, fill
=X
)
24 self
.labeldisplay
= Label(self
.master
, anchor
=W
,
25 text
="Enter a string to search:")
26 self
.labeldisplay
.pack(fill
=X
)
27 self
.labeldisplay
.pack(fill
=X
)
29 self
.showframe
= Frame(master
)
30 self
.showframe
.pack(fill
=X
, anchor
=W
)
32 self
.showvar
= StringVar(master
)
33 self
.showvar
.set("first")
35 self
.showfirstradio
= Radiobutton(self
.showframe
,
36 text
="Highlight first match",
37 variable
=self
.showvar
,
39 command
=self
.recompile
)
40 self
.showfirstradio
.pack(side
=LEFT
)
42 self
.showallradio
= Radiobutton(self
.showframe
,
43 text
="Highlight all matches",
44 variable
=self
.showvar
,
46 command
=self
.recompile
)
47 self
.showallradio
.pack(side
=LEFT
)
49 self
.stringdisplay
= Text(self
.master
, width
=60, height
=4)
50 self
.stringdisplay
.pack(fill
=BOTH
, expand
=1)
51 self
.stringdisplay
.tag_configure("hit", background
="yellow")
53 self
.grouplabel
= Label(self
.master
, text
="Groups:", anchor
=W
)
54 self
.grouplabel
.pack(fill
=X
)
56 self
.grouplist
= Listbox(self
.master
)
57 self
.grouplist
.pack(expand
=1, fill
=BOTH
)
59 self
.regexdisplay
.bind('<Key>', self
.recompile
)
60 self
.stringdisplay
.bind('<Key>', self
.reevaluate
)
65 btags
= self
.regexdisplay
.bindtags()
66 self
.regexdisplay
.bindtags(btags
[1:] + btags
[:1])
68 btags
= self
.stringdisplay
.bindtags()
69 self
.stringdisplay
.bindtags(btags
[1:] + btags
[:1])
75 for name
in ('IGNORECASE',
80 if len(self
.boxes
) % 3 == 0:
81 frame
= Frame(self
.master
)
83 self
.frames
.append(frame
)
84 val
= getattr(re
, name
)
86 box
= Checkbutton(frame
,
87 variable
=var
, text
=name
,
88 offvalue
=0, onvalue
=val
,
89 command
=self
.recompile
)
91 self
.boxes
.append(box
)
97 flags
= flags | var
.get()
101 def recompile(self
, event
=None):
103 self
.compiled
= re
.compile(self
.regexdisplay
.get(),
105 bg
= self
.promptdisplay
['background']
106 self
.statusdisplay
.config(text
="", background
=bg
)
107 except re
.error
as msg
:
109 self
.statusdisplay
.config(
110 text
="re.error: %s" % str(msg
),
114 def reevaluate(self
, event
=None):
116 self
.stringdisplay
.tag_remove("hit", "1.0", END
)
120 self
.stringdisplay
.tag_remove("hit0", "1.0", END
)
123 self
.grouplist
.delete(0, END
)
124 if not self
.compiled
:
126 self
.stringdisplay
.tag_configure("hit", background
="yellow")
127 self
.stringdisplay
.tag_configure("hit0", background
="orange")
128 text
= self
.stringdisplay
.get("1.0", END
)
131 while last
<= len(text
):
132 m
= self
.compiled
.search(text
, last
)
135 first
, last
= m
.span()
141 pfirst
= "1.0 + %d chars" % first
142 plast
= "1.0 + %d chars" % last
143 self
.stringdisplay
.tag_add(tag
, pfirst
, plast
)
145 self
.stringdisplay
.yview_pickplace(pfirst
)
146 groups
= list(m
.groups())
147 groups
.insert(0, m
.group())
148 for i
in range(len(groups
)):
149 g
= "%2d: %r" % (i
, groups
[i
])
150 self
.grouplist
.insert(END
, g
)
151 nmatches
= nmatches
+ 1
152 if self
.showvar
.get() == "first":
156 self
.statusdisplay
.config(text
="(no match)",
159 self
.statusdisplay
.config(text
="")
162 # Main function, run when invoked as a stand-alone Python program.
167 root
.protocol('WM_DELETE_WINDOW', root
.quit
)
170 if __name__
== '__main__':