-1

I want to remove every text inside < >. I used to replace('<*>',' ') but it does not remove them at all.

For example, my text is "<a/ddff>Nitin Hardeniya - 2015 - ‎Computers" I want it to turn into "Nitin Hardeniya - 2015 - ‎Computers".

Please advise me on how should I correct my code.

Here is code:

import tkinter as tk

root = tk.Tk()
text = root.clipboard_get()
def onclick():
    text_widget.delete("1.0", "end")   
    text_widget.insert(tk.END, text.replace('<*>',' '))   

root = tk.Tk()
text_widget = tk.Text(root)
text_widget.insert(tk.END, text)  #inserting the data in the text widget
text_widget.pack()

button = tk.Button(root, text = "Press me!", command = onclick)
button.pack()

root.mainloop()
Saad
  • 3,340
  • 2
  • 10
  • 32
ankerbow
  • 25
  • 8
  • Read [Why are multiple instances of Tk discouraged?](https://stackoverflow.com/a/48045508/7414759) – stovfl May 16 '20 at 17:32

1 Answers1

0

You need to use a regular expression for such matching. the <*> in your line: text.replace('<*>',' ') is not a regular expression it is a strict match (see replace function documentation)

A way of doing it is

def onclick():
    original = text_widget.get("1.0", "end")   
    pattern = re.compile(r'(<[^<>]+>)')
    fixed= pattern.sub(r" ", original )
    text_widget.delete("1.0", "end")   
    text_widget.insert(tk.END, fixed)   
Jean-Marc Volle
  • 3,113
  • 1
  • 16
  • 20