In my Java code, I have a String variable that contains HTML code like this:
<span style="text-decoration: underline;">test</span>
My goal is to convert it to look like this:
<u>test</u>
If the HTML code is like this:
<span style="color: #2873ee; text-decoration: underline;">test</span>
I want it to be transformed into:
<font color="#2873ee"><u>test</u></font>
I am currently using regex for this purpose:
affectedString.replaceAll("<span style=\"text-decoration: underline;\">(.*?)<\\/span>", "<u>$1</u>");
and
affectedString.replaceAll("<span style=\"color:\\s*?(#[A-Fa-f0-9]{6}|[A-Fa-f0-9]{3}); text-decoration: underline;\">(.*?)<\\/span>", "<u><font color=\"$1\">$2</u></font>");
However, there are some issues with this approach and I find it cumbersome. I have to specify each case of coincidence which is not efficient. Additionally, the regex does not work well with nested spans like in the example:
<span style="text-decoration: underline;">test <span style="text-decoration: line-through;">two</span></span>
The result becomes incorrect due to regex matching issues.
I'm seeking advice on a better solution or an improved regex pattern to handle these scenarios. Any suggestions would be appreciated. Thank you.