简体   繁体   中英

Search tkinter text widget contents using regular expression

In tkinter text widget , how to search for a whole word. I tried to use the following syntax, but it didn't match anything although the word already exists:

index = self.text.search(r'\b%s\b' % myWord, INSERT, backwards=True, regexp=True)

Any hints?

The regular expression specified is interpreted by tcl , not by python .

Tcl use different syntax for word boundary: \\y instead of \\b . (See Word boundaries , especially Tcl Word Boundaries part.)

The line should be replaced with:

index = self.text.search(r'\y%s\y' % myWord, INSERT, backwards=True, regexp=True)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM