I'm trying to remove some phrases out of a string not dependent on letter case, but there are some words/phrases ending with "&" that should remove everything including the word after the connected word/phrase.
import re
txt = "People have a different titles, like Dr. Mr. Miss and so on."
removed = "like", "Different titles,", "a", "Miss and&"
char = "&"
removed = list(sorted(removed))
for i in range(len(removed)):
removed[i] = removed[i].upper()
txt = re.sub(fr"\s*(?<!\S)(?:{'|'.join(map(re.escape, removed))})(?!\S)", "", txt.upper()).strip()
This gives me "PEOPLE HAVE DR. MR. MISS AND SO ON.". Is there any way using regex to have the result as "PEOPLE HAVE DR. MR."? If not with regex then how to remove phrases from a string? At the moment I have tried it like this, unfortunately to no avail.
txt = "People have a different titles, like Dr. Mr. Miss and so on."
remove = "like", "a", "&Miss and", "&so"
ch = "&"
txt = txt.split()
new = []
for i in txt:
if ch+i in remove:
break
else:
if i not in remove:
new.append(i)
result = " ".join(new)
I get the result "People have different titles, Dr. Mr. Miss and", whereas I would like to get "People have different titles, Dr. Mr."