Python Forum
[Tkinter] Text.search() regexp not working
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Text.search() regexp not working
#1
My code below uses text_widget.search() with regexp=True but it's not finding my target word. Can someone tell me what I am missing?

I also have a non-regexp search which works and highlights the text widget found words. However, the regexp line doesn't find the words (idx returns "") and I'd like to use the regexp version because the non-regexp search() method does not have a whole word flag.

Thank you...


from tkinter import *

class Application(object):

    def __init__(self, main_win):
        self.main_win = main_win

def search(text_widget, keyword, tag):
    pos = '1.0'
    while True:
        #idx = text_widget.search(keyword, pos, "end-1c", nocase=0, exact=1)  # nocase=1 ignores case
        idx = text_widget.search('/' + keyword +'/g', pos, "end-1c", regexp=True)
        if not idx:
            break
        pos = '{}+{}c'.format(idx, len(keyword))
        text_widget.tag_add(tag, idx, pos)

def main():
    win = Tk()
    app = Application(win)

    txt = Text(win, bg='cyan')
    txt.pack()
    txt.insert('end-1c', 'import\n' \
    'Import\n'\
    'Impo\n'\
    'impo\n')

    txt.tag_config('passed', background='yellow')
    search(txt, 'impo', 'passed')

    win.mainloop()

if __name__ == '__main__':
    main()
Reply
#2
Did you only want it to highlight the full word impo and not impo of import.
If so the following seems to work.
idx = text_widget.search(f'^{keyword}$', pos, "end-1c", regexp=True)
https://www.tutorialspoint.com/tcl-tk/tc...ssions.htm Wrote:^ Beginning string should match.
$ Ending string should match.
Reply
#3
'^{keyword}$' finds the word but idx returns ''

If I remove the ^ and $ then idx returns a value (and that lets me highlight the target word in my text widget).
Reply
#4
idx = text_widget.search(f'^{keyword}$', pos, "end-1c", regexp=True)
print(idx)
returns
Output:
4.0
for me
Reply
#5
I'll have to dig deeper in my project and see why it works for you and not me.

Thank you for the help...
Reply
#6
One more question if I may: how do i add the case insensitive flag?
Reply
#7
You already know how to, you have it in your code # nocase=1 ignores case
Reply
#8
Yes but that is for the non-regexp usage an I couldn't get it to work.

I discovered '(?i){keyword}' works with your code snippet regexp.

Thank you...
Reply
#9
idx = text_widget.search(f'^{keyword}$', pos, "end-1c", regexp=True, nocase=1)
print(idx)
Output:
3.0 4.0
Reply
#10
Here's a complete test app showing that once you add the ^ and $ idx returns '' and no word highlighting.

BTW I'm running python 3.6, could that be the reason?

from tkinter import *

class Application(object):

    def __init__(self, main_win):
        self.main_win = main_win

def search(text_widget, keyword, tag):
    pos = '1.0'
    case_insensitive = '(?i)'
    whole_word_left = '^'
    whole_word_right = '$'
    while True:
        idx = text_widget.search(f'{case_insensitive}{whole_word_left}{keyword}{whole_word_right}', pos, "end-1c", regexp=True)
        if not idx:
            break
        print(idx)
        pos = '{}+{}c'.format(idx, len(keyword))
        text_widget.tag_add(tag, idx, pos)

def main():
    win = Tk()
    app = Application(win)
    win.geometry("800x600+-1000+300")

    txt = Text(win, bg='cyan')
    txt.pack()
    txt.insert('end-1c', 'import\nimport time\nfrom time import sleep\nImpo signal\nImport sys\nimpo board\nimport busio\nImpo digitalio\nfrom digitalio import Direction, Pull\nfrom RPi import GPIO\n10from adafruit_mcp230xx.mcp23017 import MCP23017\n')

    txt.tag_config('passed', background='yellow')
    search(txt, 'impo', 'passed')

    win.mainloop()

if __name__ == '__main__':
    main()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Search data in treeview without search button TomasSanchexx 3 1,529 Aug-12-2023, 03:17 AM
Last Post: deanhystad
  [Tkinter] Paste Operation not working in Text Widget Code_Enthusiast 1 2,910 Sep-11-2019, 08:49 PM
Last Post: Larz60+
  [Tkinter] add search bar = search for input in all computer directory francisco_neves2020 15 10,769 Apr-14-2019, 07:29 PM
Last Post: francisco_neves2020

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020