Question: As a premise, I have an HTML text, with some `<ol>` elements. These have a `start` attribute, but the framework I'm using is not capable to interpret them during a PDF conversion. So, the trick I am trying to apply is to add a number of invisible `<li>` elements at the beginning.

As an example, suppose this input text:

```
<ol start="3">
   <li>Element 1</li>
   <li>Element 2</li>
   <li>Element 3</li>
</ol>

```

I want to produce this result:

```
<ol>
   <li style="visibility:hidden"></li>
   <li style="visibility:hidden"></li>
   <li>Element 1</li>
   <li>Element 2</li>
   <li>Element 3</li>
</ol>

```

So, adding n-1 invisible elements into the ordered list.
But I'm not able to do that from Java in a generalized way.

Supposing the exact case in the example, I could do this (using `replace`, so - to be honest - without regex):

```
htmlString = htmlString.replace("<ol start=\"3\">",
            "<ol><li style=\"visibility:hidden\"></li><li style=\"visibility:hidden\"></li>");

```

But, obviously, it just applies to the case with "start=3". I know that I can use groups to extract the "3", but how can I use it as a "variable" to specify the string `<li style=\"visibility:hidden\"></li>` n-1 number of times?
Thanks for any insight.

Answer: 
You cannot do this using regular expressions, or even if you find some hack to do this it's going to be a suboptimal solution..

The right way to do this is to use an HTML parsing library (e.g. [Jsoup](https://jsoup.org/)) and then add the `<li>` tags as children to the `<ol>`, specifically using the [Element#prepend](https://jsoup.org/apidocs/org/jsoup/nodes/Element.html#prepend-java.lang.String-) method. (With Jsoup you can also read the `start` attribute value in order to compute how many elements to add)
Please use java **Matcher** and **Pattern** to count the occurrence of **li** tag and use **StringBuilder** **insert** method to insert invisible elements.

```
Matcher m = Pattern.compile("<li>").matcher(s);
        while(m.find()){
           ++count;
        }

```