I started to reply with basic instructions, but I think you've got the idea.
Note that if your text contains curly brackets (as you stated), VA will treat the contents as a token. To get around that part, you'll need to select the, 'Do not render tokens in variable values' option. Of course, I don't think that is followed when using the length/pos tokens... I think you would need to escape the brackets first by using |{ and |}.
Also, if your text contains %, VA will try to expand environment variables (which may have an impact). Now that I'm looking at this, that should be an option as well.
Seems real messy, but these are for use with simple situations. For more complex stuff (like this situation, possibly), you'd want to take a look at inline functions.