I'm using RegEx in Python to search through a text file for occurrences of names in a roster, and then append a "!" character to the start of the string. For example:
roster = ["name1," "name2," "name3"]
Original String = "name1 went home."
Output String - "!name1 went home."
I found this thread on how to append to the end of the string, which I used successfully for that purpose. I've tinkered with RegEx to append at the start of the string, but with no success. My attempt is below - any recommendations?
with open("File.txt", 'r+') as f:
s = f.read()
new_s = re.sub(r'^(.*{}.*)^'.format(re.escape("name1")), lambda g: g.group(0) + "!", s, flags=re.MULTILINE)
f.seek(0)
f.write(new_s)