Python Forum

Full Version: Text.search() regexp not working
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
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()
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.
'^{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).
idx = text_widget.search(f'^{keyword}$', pos, "end-1c", regexp=True)
print(idx)
returns
Output:
4.0
for me
I'll have to dig deeper in my project and see why it works for you and not me.

Thank you for the help...
One more question if I may: how do i add the case insensitive flag?
You already know how to, you have it in your code # nocase=1 ignores case
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...
idx = text_widget.search(f'^{keyword}$', pos, "end-1c", regexp=True, nocase=1)
print(idx)
Output:
3.0 4.0
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()
Pages: 1 2