Search for contents of tkinter text widget using regular expression

In tkinter text widgethow to search for a whole word. I tried using the following syntax, but it did not match anything, although the word already exists:

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

Any clues?

+3
source share
1 answer

The specified regular expression is interpreted tcl, not python.

Tcl uses a different syntax for word boundaries: \yinstead \b. (See Word boundaries , especially the Tcl part of Word Boundaries.)

The line should be replaced by:

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

All Articles