title: Code golf: "Color highlighting" of repeated text
body: <p>(Thanks to greg0ire below for helping with key concepts)</p>
  
  <p>The challenge:
  Build a program that finds all substrings and "tags" them with color attributes (effectively highlighting them in XML). </p>
  
  <p>The rules:</p>
  
  <ol>
  <li>This should only be done for substrings of length 2 or more.  </li>
  <li>Substrings are just strings of consecutive characters, which may include non-alphabetic characters. Note that spaces and other punctuation do not delimit substrings.</li>
  <li>Character casing cannot be ignored.</li>
  <li>The "highlight" should be done by tagging the substring in XML. Your tagging should be of the form <code>&lt;TAG#&gt;theSubstring&lt;/TAG#&gt;</code> where <code>#</code> is a positive number unique to that substring and identical substrings.</li>
  <li>The priority of the algorithm is to find the longest substring, not how many times it matches within the text.</li>
  </ol>
  
  <p>Note: The order of the tagging shown in the example below is not important. Its just used by the OP for clarity.</p>
  
  <hr>
  
  <p>An example input:</p>
  
  <pre><code>LoremIpsumissimplydummytextoftheprintingandtypesettingindustry.LoremIpsumhasbeentheindustry'sstandarddummytexteversincethe1500s,whenanunknownprintertookagalleyoftypeandscrambledittomakeatypespecimenbook.
  </code></pre>
  
  <hr>
  
  <p>A partially correct output (OP may NOT have completely replaced perfectly in this example)</p>
  
  <pre><code>&lt;TAG1&gt;LoremIpsum&lt;/TAG1&gt;issimply&lt;TAG2&gt;dummytext&lt;/TAG2&gt;of&lt;TAG5&gt;the&lt;/TAG5&gt;&lt;TAG3&gt;print&lt;/TAG3&gt;ingand&lt;TAG4&gt;type&lt;/TAG4&gt;setting&lt;TAG6&gt;industry&lt;/TAG6&gt;.&lt;TAG1&gt;LoremIpsum&lt;/TAG1&gt;hasbeen&lt;TAG5&gt;the&lt;/TAG5&gt;&lt;TAG6&gt;industry&lt;/TAG6&gt;'sstandard&lt;TAG2&gt;dummytext&lt;/TAG2&gt;eversince&lt;TAG5&gt;the&lt;/TAG5&gt;1500s,whenanunknown&lt;TAG3&gt;print&lt;/TAG3&gt;ertookagalleyof&lt;TAG4&gt;type&lt;/TAG4&gt;andscrambledittomakea&lt;TAG4&gt;type&lt;/TAG4&gt;specimenbook.
  </code></pre>
  
  <hr>
  
  <p>Your code should be able to handle edge cases, such as the following:</p>
  
  <p>Example Input 2:</p>
  
  <pre><code>hello!TAG!&lt;/hello.TAG.&lt;/
  </code></pre>
  
  <p>Example Output 2:</p>
  
  <pre><code>&lt;TAG1&gt;hello&lt;/TAG1&gt;!&lt;TAG2&gt;TAG&lt;/TAG2&gt;!&lt;TAG3&gt;&lt;/&lt;/TAG3&gt;&lt;TAG1&gt;hello&lt;/TAG1&gt;.&lt;TAG2&gt;TAG&lt;/TAG2&gt;.&lt;TAG3&gt;&lt;/&lt;/TAG3&gt;
  </code></pre>
  
  <hr>
  
  <p>The winner:</p>
  
  <ul>
  <li>Most elegant solution wins (judged by
  others comments, upvotes) </li>
  <li>Bonus
  points/consideration for solutions
  utilizing shell scripting</li>
  </ul>
  
  <hr>
  
  <p>Minor clarifications:</p>
  
  <ul>
  <li>Input can be hard coded or read from a file</li>
  <li>The criteria remains "elegance", which admittedly IS slightly vague, but it also encapsulates simple character/line counts as well. Comments by others and/or upvotes are also indicative of how the SO community views the challenge</li>
  </ul>