title: How to solve the string replace fiasco
body: <p><strong>NOTE</strong> : My problem is NOT that my links are not being replaced. But, it's being <strong>NESTED</strong>.
  eg, this is the comment</p>
  
  <pre><code>some string with www.google.com/blah/blah also something else www.google.com
  </code></pre>
  
  <p>by the time second string replace is done, part of first one is also valid (<strong>www.google.com</strong>/blah/blah) so it's replacing that link twice.</p>
  
  <p>I have a web app which lets users comment.
  I am processing the input string and converting all links to html link format <strong>when I display it on the page</strong>. Original user input string stays in DB and nothing ever happens so it's not corrupted over processing. Just when I show that on page, I do my function on it.</p>
  
  <p>Now, this is the logic I am using to replace all links with their html formats</p>
  
  <ol>
  <li>Regex all links</li>
  <li>For each match, replace link with it's html format version in the original string.</li>
  <li>Finally display string.</li>
  </ol>
  
  <p>ex: <code>www.google.com</code> becomes <code>&lt;a href="http://www.google.com" target="_blank"&gt;www.google.com&lt;/a&gt;</code> just before it's displayed on page.</p>
  
  <p>This was working great until recently, one of my customer posted a content with two links from same domain.</p>
  
  <p>the links were, say,</p>
  
  <ol>
  <li>www.google.com/images/blahblah</li>
  <li>www.google.com</li>
  </ol>
  
  <p>My problem is, when the second time around, a string replace is done (I am using <code>StringBuilder.Replace</code>) the first link gets replaced as well!</p>
  
  <p>so, firstly,</p>
  
  <pre><code>www.google.com/images/blahblah
  </code></pre>
  
  <p>becomes</p>
  
  <pre><code>&lt;a href="http://www.google.com/images/blahblah" target="_blank"&gt;www.google.com/image/blahblah&lt;/a&gt;
  </code></pre>
  
  <p>which is well. But the problem arises for second string replace, since replace is global, it does a replace on already processed link so the original (above) link becomes twisted as it sees <strong>www.google.com</strong> in there as well.</p>
  
  <p>This is messing up so much that I actually get a mutilated abomination of a string. </p>
  
  <p>How do I avoid this?</p>
  
  <p>Does the <code>Regex.Matches</code> provide an index of matched element for me to work with? I couldn't find it anywhere.</p>
  
  <p>What's the best way to deal with? any suggestions? </p>
  
  <p>sorry for lengthy question.</p>
  
  <p>I can prolly do this by manually traversing string but it's long and painful there's got to be a good way to do it...</p>
  
  <p><strong>edit</strong>  adding extra info as someone asked: </p>
  
  <p>My regex: </p>
  
  <pre><code>    string rPattern = @"(((http|ftp|https):\/\/)|www\.)[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&amp;amp;:/~\+#!]*[\w\-\@?^=%&amp;amp;/~\+#])?";
  
       Regex rLinks = new Regex(rPattern, RegexOptions.IgnoreCase);
       MatchCollection matches = rLinks.Matches(inputString);
  </code></pre>
  
  <p>then I am using</p>
  
  <pre><code>foreach(Match match in matches)
  {
      if(match.value.StartsWith("www.youtube.com/watch"))
      {
           //logic to embed youtube video - this works fine.
      } 
  }
  
  //Here I replace all hyperlinks to their &lt;a href&gt; parts
  </code></pre>