Drawing a box around sub-strings of a document
<p>I was hoping to get some help in how I should approach a program I have attempted to write a few times now.</p>

<p>I have a number of folders. In each folder, there is a HTML file, and a .txt file which contains text in the HTML file, stripped of all HTML tags.</p>

<p>As an example, a simplified HTML file may be </p>

<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;html&gt;&lt;head&gt;&lt;/head&gt;&lt;body&gt;&lt;p&gt;This is some &lt;b&gt;text&lt;/b&gt;&lt;/p&gt;&lt;p&gt;Please ignore me&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</code></pre>
</div>
</div>
</p>

<p>And within a .txt in the same folder, I have "This is some text".</p>

<p>From these two files, I would like to create a new file which is a HTML with a box drawn around "This is some text", like so :</p>

<p><a href="https://i.stack.imgur.com/MAIIj.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/MAIIj.png" alt="Image of the HTML file with a box crudely drawn around &quot;This is some text&quot;"></a></p>

<p>The obvious problem here is that the pretty-printed text files do not contain any mark-up, and so finding it within the HTML document is difficult.</p>

<p>My idea thus far has been : </p>

<p>-Save the .txt contents in a variable.
-Grab the HTML contents, strip of all HTML tags :</p>

<pre><code>public static String html2text(String html) {
    return Jsoup.parse(html).text();
}
</code></pre>

<p>I'm unsure how to proceed from this point. I mean...I could try to add a div with a class surrounding the text, and then add a border style to this...but how do I find the sub-string in the HTML reliably, retaining all of the markup within the HTML ? </p>

<p>I'm sure there is a simple way to do this and I am just overthinking it, I would usually have a chat with a friend about this and solve it but everyone seems to be offline - so I come to you for guidance here.</p>

<p>Can anyone offer any feedback please? Thanks.</p>