I don't really get how could your code solve the requirements you explained above...
That said, it seems to be a easier way to do this kind of job by using the JAVA's replaceAll
method, typically for the two-letter words:
"this is awesome look at this two letter words get replaced"
.replaceAll("(\\b\\w{2}\\b)", "<h1>$1</h1>");
This prints:
this <h1>is</h1> awesome look <h1>at</h1> this two letter words get replaced