Assuming your language doesn't allow nesting tags inside other tags, using REGEX to search and replace should work in this scenario.
Java has a Pattern and Matcher class which you could use to compile a pattern and then use the Matcher to loop through all the matches and replace them with content.
This SO example actually does a great job explaining it:
stackoverflow.com/questions/9605716/java-regular-…
If your language allows nesting tags (example would be an if tag inside a loop tag inside another if tag), then it gets a bit messy, you'll need to start building parse trees and recursively do regex matches and store results in a map that simulates the scopes you're trying to create with the nested tags. It's a less trivial exercise, but also doable with REGEX.