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