Note that there are some explanatory texts on larger screens.

plurals
  1. POSplit tokens on string using Regex in c#
    primarykey
    data
    text
    <p>I have some "tokenized" templates, for example (I call <em>tokens</em> the part between double braces): </p> <pre class="lang-cs prettyprint-override"><code>var template1 = "{{TOKEN1}} is a {{TOKEN2}} and it has some {{TOKEN3}}"; </code></pre> <p>I want to extract an array from this sentence, in order to have something like:</p> <pre class="lang-cs prettyprint-override"><code>Array("{{TOKEN1}}", " is a ", "{{TOKEN2}}", " and it has some ", "{{TOKEN3}}"); </code></pre> <p>I've tried to achieve that with the following Regex code:</p> <pre class="lang-cs prettyprint-override"><code>Regex r = new Regex(@"({{[^\}]*}})"); var n = r.Split(template1); </code></pre> <p>And the result is:</p> <pre class="lang-cs prettyprint-override"><code>Array("", "{{TOKEN1}}", " is a ", "{{TOKEN2}}", " and it has some ", "{{TOKEN3}}", ""); </code></pre> <p>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.</p> <p>The issue I'm currently facing is the extra empty term in the beginning and/or in the end of the array <strong>when the first and/or last terms on the template are "tokens"</strong>. Why is it happening? Am I doing something wrong, or I should I always check these two positions for emptiness?</p> <p>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!</p> <p>Thank you!</p> <p><strong>Edit:</strong> as requested, I'll post a simple example to why do I need this distinction on tokens and text.</p> <pre class="lang-cs prettyprint-override"><code>public abstract class TextParts { } public class TextToken : TextParts { } public class TextConstant : TextParts { } var list = new List&lt;TextParts&gt;(); list.Add( new TextToken("{{TOKEN1}}") ); list.Add( new TextConstant(" is a ") ); list.Add( new TextToken("{{TOKEN2}}") ); /* and so on */ </code></pre> <p>This way, I'll have a list of the parts that composes my string and I'll be able to record that on my database to allow future manipulation and substitution. In fact, each of this TOKEN will be replaced by a Regex string.</p> <p>The objective is that users will be able to input messages like "{{SERVER}} is not listening on port {{PORT}}", and I'll be able to replace "{{SERVER}}" to <code>[a-zA-Z0-9 ]+</code> and "{{PORT}}" to <code>\d{1,5}</code>. Makes sense?</p> <p>I hope this makes the post more clear.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload