title: Is it possible to remove tags (or sequences) and relate or remember them as indexes?
body: <p>I'm working with HTML tags, and I need to interpret HTML documents. Here's what I need to achieve:</p>
  
  <ul>
  <li>I have to recognize and remove HTML tags without removing the
  original content.</li>
  <li>I have to store the index of the previously existing markups.</li>
  </ul>
  
  <p>So here's a example. Imagine that I have the following markup:</p>
  
  <pre><code>This &lt;strong&gt;is a&lt;/strong&gt; message.
  </code></pre>
  
  <p>In this example, we have a String sequence with 35 characters, and markedup with <code>strong</code> tag. As we know, an HTML markup has a start and an end, and if we interpret the start and end markup as a sequence of characters, each also has a start and an end (a character index). </p>
  
  <p>Again, in the previous example, the beggining index of the open/start tag is 5 (starts at index 0), and the end index is 13. The same logic goes to the close tag.</p>
  
  <p>Now, once we remove the markup, we end up with the following:</p>
  
  <pre><code>This is a message.
  </code></pre>
  
  <p><strong>The question:</strong></p>
  
  <p>How can I remember with this sequence the places where I could enter the markup again?</p>
  
  <p>For example, once the markup has been removed, how do I know that I have to insert the opening tag in the X position/index, and the closing tag in the Y position/index... Like so:</p>
  
  <pre><code>This is a message.
       5   9
  index 5 = &lt;strong&gt;
  index 9 = &lt;/strong&gt;
  </code></pre>
  
  <p><strong>I must remember that it is possible to find the following situation:</strong></p>
  
  <pre><code>&lt;a&gt;T&lt;b attribute="value"&gt;h&lt;c&gt;i&lt;d&gt;s&lt;/a&gt; &lt;g&gt;i&lt;h&gt;s&lt;/h&gt;&lt;/g&gt; &lt;/b&gt;a&lt;/c&gt; &lt;e&gt;t&lt;/e&gt;e&lt;f&gt;s&lt;/d&gt;t&lt;/f&gt;.
  </code></pre>
  
  <p>I need to implement this <strong>in Java</strong>. I've figured out how to get the start and end index of each tag in a document. For this, I'm using regular expressions (Pattern and Matcher), but I still do not know how to insert the tags again properly (as described). I would like a working example (if possible). It does not have to be the best example (the best solution) in the world, but only that it <strong>works the right way for any kind of situation</strong>.</p>
  
  <p>If anyone has not understood my question, please comment that I will do it better.</p>
  
  <p>Thanks in advance.</p>
  
  <h2>EDIT</h2>
  
  <p>People in the comments are saying that I should not use regular expressions to work with HTML. I do not care to use or not regular expressions to solve this problem, I just want to solve it, no matter how (But of course, in the most appropriate way).</p>
  
  <p>I mentioned that I'm using regular expressions, but I do not mind using another approach that presents the same solution. I read that a XML parser could be the solution. Is that correct? Is there an XML parser capable of doing all this what I need?</p>
  
  <p>Again, Thanks in advance.</p>
  
  <h2>EDIT 2</h2>
  
  <p>I'm doing this edition now to explain the <strong>applicability</strong> of my problem (as asked). Well, before I start, I want to say that what I'm trying to do is something I've never done before, it's not something on my area, so it may not be the most appropriate way to do it. Anyway...</p>
  
  <p>I'm developing a site where users are allowed to read content but can not edit it (edit or remove text). However, users can still mark/highlight excerpts (ranges) of the content present (with some stylization). This is the big <strong>summary</strong>.</p>
  
  <p>Now <strong>the problem is how to do this</strong> (in Java). On the client side, for now, I was thinking of using <a href="https://www.tinymce.com/" rel="nofollow noreferrer">TinyMCE</a> to enable styling of content without text editing. I could save stylized text to a database, but this would take up a lot of space, since every client is allowed to do this, given that they are many clients. So if a client marks snippets of a paragraph, saving the paragraph back in the database for each client in the system is somewhat costly in terms of memory.</p>
  
  <p>So I thought of just saving the range (indexes) of the markups made by users in a database. It is much easier to save just a few numbers than all the text with the styling required. In the case, for example, I could save a line / record in a table that says:</p>
  
  <ul>
  <li>In X paragraph, from Y to Z index, the user P defined a ABC
  stylization.</li>
  </ul>
  
  <p>This would require a translation / conversion, from database to HTML, and HTML to database. Setting a converter can be easy (I guess), but I do not know how to get the indexes (following this logic). And then we stop again at the beginning of my question. </p>
  
  <hr>
  
  <p>Just to make it clear: </p>
  
  <p>If someone offers a solution that will cost money, such as a paid API, tool, or something similar, unfortunately this option is not feasible for me. I'm sorry :/</p>
  
  <p>In a similar way, I know it would be ideal to do this processing with JavaScript (client-side). It turns out that I do not have a specialized JavaScript team, so this needs to be done on the server side (unfortunately), which is written in Java. I can only use a JavaScript solution if it is already ready, easy and quick to use. Would you know of any ready-made, easy-to-use library that can do it in a simple way? Does it exist?</p>