title: Searching in an indexed string
body: <h1>The plot</h1>
  
  <p>There is a rather complicatedly formatted string, like there's no such readable regex that parses it. And the aim is to get a specific substring for example, and to get it's original position. That substring is reached after parsing a bit, like trimming, removing the beginning something and searching the n-th element for example. I just want to demonstrate you the complexity with this example, otherwise it's pretty general.</p>
  
  <p>For demonstration, see this rudimentary example. The way it is isn't really important, just to reach a pretty complicated parse model. Obviously, there can be more rule and you can write a simplier model as well.</p>
  
  <ul>
  <li>FirstBlock{Index1, Index2} SecondBlock ThirdBlock</li>
  <li>{ FirstBlock {Index1,Index2} SecondBlock}</li>
  <li>{FirstBlock SecondBlock ThirdBlock FourthBlock}</li>
  </ul>
  
  <p>I've tried to make as random as it could be. The parsing model is like:</p>
  
  <pre><code>string text = "{ FirstBlock {Index1,Index2} SecondBlock}";
  
  text = text.Trim();
  
  if (text.First() == '{')
  {
      text = text.SubString(1, text.Length - 2);
  }
  
  text = text.Trim();
  
  string firstBlock = text.Split(new char[] { ' ', '{' })[0];
  
  text = text.Remove(0, firstBlock.Length).Trim();
  
  string indices = "";
  
  if (text.First() == '{')
  {
      indices = text.Split(new char[] { '{', '}' })[0];
      text = text.Remove(0, indices.Length).Trim();
  }
  
  string[] blocks = text.Split(' ');
  </code></pre>
  
  <h1>The easy way</h1>
  
  <p>There is a way that is pretty easy to implement and straightforward. But does not give you the correct result sometimes. That way you parse the string and get the substring and then you make a re-search, for example <code>string.IndexOf()</code> and get the position. But if there are two match for example, you are given the first one even though it is not sure you wanted that one.</p>
  
  <h1>My notion</h1>
  
  <p>The way I think is quite elegant but still not consummate is to index the characters of the string at the beginning, then parse it, and eventually you end up with the proper characters and their position also. My problem there is that then you can't really use the functions the library gives, and I don't know a way to do that. Using the snippet above:</p>
  
  <pre><code>List&lt;Tuple&lt;int, char&gt;&gt; indexedText = text
                  .Select((ch, index) =&gt; new Tuple&lt;int, char&gt;(index, ch))
                  .ToList();
  </code></pre>
  
  <p>And with this structure you can still process the string without library methods but you are given the position indices eventually. For example, trim:</p>
  
  <pre><code>indexedText = indexedText
                  .SkipWhile(indexedChar =&gt; char.IsWhiteSpace(indexedChar.Item2))
                  .ToList();
  </code></pre>
  
  <h1>The actual question</h1>
  
  <p>The question can either be a new solution or the way you can use library methods with indexed strings. The aim is to get the indices back after parsing a string. It is possible that there is a very simple way that is just out of my scope but I haven't found a proper solution yet. The solution I don't want is to simplify the parsing system, as I said it is just for demonstration.</p>