regex - python - Tkinter - How can I write Tkinter reg. expression to fetch the string after a word? -


[problem] how can write tkinter reg. expression fetch string after 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 replace: "cut-off: xyz" if: - there no text before "cut-off: xyz" - there text before "cut-off: xyz" text not include chars "cut-off: xyz" 

[desired] - code should replace "cut-off: xyz" regardless of position

[note] - if replace reg exp static string ("cut-off"), not face problems - python regular expression: "(cut-off.*)" not deliver expected output

the tkinter text widget search function can return finds, , how many characters found. can search via regular expression, can't return individual groups within expression such text after word.

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

the search mechanism return index of match. can optionally return number of characters matched, makes possible select matched.

for example, if want replace begins "cut-off: ", can this:

import tkinter 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) 

Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -