I am making an addon and i bumped into the following problem:
for example i have the following string

This is an example <string> and i <string> think it is a cool <string>

The problem is:
i want to replace the <string> on the follwing pattern
string1
if (string1 is already placed) { string2 } // i give this for example how i want the strings to be placed.
if (string2 is already placed) { string1}

it is something like if you have a CTRL+B code and you want to change it to html with <b> </b>
to check if it is open and if it is close it.

The only way i could think of was to loop in the tokens but it has 2 main problems.
first it would be rather slow
second: if it comes <string>blabla<string> it would put string1/2 on both

any other ideas how to do it ?