I have some "tokenized" templates, for example (I call tokens the part between double braces):
var template1 = "{{TOKEN1}} is a {{TOKEN2}} and it has some {{TOKEN3}}";
I want to extract an array from this sentence, in order to have something like:
Array("{{TOKEN1}}",
" is a ",
"{{TOKEN2}}",
" and it has some ",
"{{TOKEN3}}");
I've tried to achieve that with the following Regex code:
Regex r = new Regex(@"({{[^\}]*}})");
var n = r.Split(template1);
And the result is:
Array("",
"{{TOKEN1}}",
" is a ",
"{{TOKEN2}}",
" and it has some ",
"{{TOKEN3}}",
"");
The first issue was that I was not able to recover the tokens from the sentence. I solved this just by adding the parentheses on the Regex expression, even though I'm not sure why does it solves this.
The issue I'm currently facing is the extra empty term in the beginning and/or in the end of the array when the first and/or last terms on the template are "tokens". Why is it happening? Am I doing something wrong, or I should I always check these two positions for emptiness?
On my code, I will need to know which term came from a token and which was a fixed position on the template. On this solution, I will have to check every array's position for a string starting with "{{" and ending with "}}", which I don't think is the best possibility. So, if someone comes up with a better solution to break these things apart, I'll be glad to know!
Thank you!
Edit: as requested, I'll post a simple example to why do I need this distinction on tokens and text.
pub