0

[PROBLEM] How can I write Tkinter reg. expression to fetch the string after a word ?

[INPUT]

def addCutOffFromButton():
        fieldText = afterCutOff.get()
        searchText =  '[cut\-off:.*]'
        replaceText = 'cut-off:' + str(fieldText)
        startPosition = textPad.search(searchText, "1.0", stopindex=END, regexp=True)
        print "startPosition", '{!r}'.format(startPosition)
        print len(searchText)
        if startPosition:
            endPosition = '{}+{}c'.format(startPosition, len(searchText))
            print "endPosition", '{!r}'.format(endPosition)
            textPad.delete (startPosition, endPosition)
            textPad.insert (startPosition, replaceText)
        else:
            textPad.insert(END+'-1c', '\n' + 'cut-off:' + str(fieldText)) 

[OUTPUT]

This code will replace: "cut-off: xyz" if:
- there is no text before "cut-off: xyz"
- there is text before "cut-off: xyz" but that text does not include the chars from "cut-off: xyz"

[DESIRED] - the code should replace "cut-off: xyz" regardless of its position

[NOTE] - If i replace the reg exp with static string ("cut-off"), then I will not face any problems - python regular expression: "(cut-off.*)" will not deliver expected output

2
  • The first part of the question asks about finding text after a word, but later in the question you mention replacing text. Are you wanting to get the word after a pattern, or replace a word after a pattern, or replace the pattern? Also, are the brackets literally part of what you're searching for, or is that an attempt at writing a regular expression? It's not clear what you want. Commented Oct 22, 2015 at 14:58
  • Hi. there are 2 main parts: find a text and replace it by other text. I created the question related to reg exp because this is the root cause for my problem. As I mentioned, If i replace the reg exp with static string ("cut-off"), then I will not face any problems Commented Oct 22, 2015 at 19:47

1 Answer 1

1

The tkinter text widget search function can only return what it finds, and how many characters it found. It can search via a regular expression, but it can't return individual groups from within that expression such as the text after a word.

The regular expression syntax is close to the normal python regular expression syntax, but it's not identical. The syntax is documented on the tcl/tk re_syntax man page.

The search mechanism will return the index of a match. It can optionally return the number of characters matched, which makes it possible to select everything that matched.

For example, if you want to replace everything that begins with "cut-off: ", you can do something like this:

import tkinter as tk
...
countVar = tk.IntVar()
startPosition = textPad.search(r'cut-off: .*', "1.0", count=countVar, regexp=True)
textPad.delete(startPosition, "%s+%sc" % (startPosition, countVar.get()))
textPad.insert(startPosition, replaceText)
Sign up to request clarification or add additional context in comments.

3 Comments

I tried your suggestion. The python idle returned: "Exception in Tkinter callback Traceback (most recent call last): File "C:\Python27\lib\lib-tk\Tkinter.py", line 1536, in call return self.func(*args) File "C:\automat\test", line 377, in addCutOffFromButton textPad.delete(startPosition, "%s+%sc" % (startPosition, countVar.get())) File "C:\Python27\lib\lib-tk\Tkinter.py", line 2971, in delete self.tk.call(self._w, 'delete', index1, index2) TclError: bad text index """. \n. note: I use python 2.7.10. I will keep investigating....
@user3438538: if the index is "", it didn't find the pattern.
thanks Bryan for helping me out. I spotted out the problem. there was a bonus white space after the colon. status: problem solved

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.