Spoiler: Yes this is an assignment. It is solved, but for personal interest I want to know the below.
So at the moment working with a syntax marker for an assignment, in which we input a file, and using a dictionary of regexes, colour them (keywords) accordingly.
Having some issues, though.
for i in iterations:
colour + r'\1' + colour_end
You may use capturing and backreferences:
$1<color>$2</color>$3<color>$4</color>$5. See the regex demo.
Here, the expression is split into 5 subparts with
(...) capturing groups. In the replacement pattern, those values captured are referred to with backreferences having
n format where
n is the ID of the capturing group inside the pattern.
If you have no chance to run 1 regex with multiple capturing groups, run two on end:
$1<color>for</color>(see this demo)
$1<color>in</color>(see another demo).
The single capturing group is around the part before the word, and the part after the word is not matched but checked with a positive lookahead.