It depends on what the input string ends up being. Your list of 'conditions' is not adequate.
Any HTML, really
Okay, then, you need a heck of a lot more information about which font size style needs to be changed. All of them, anywhere in the document? As in, 'take the input HTML, find any and all spans anywhere with a style attribute, then parse those style attributes to find any font-size CSS keys, and add !important to those.
Then the answer is very, very tricky. HTML is not regular and thus cannot be parsed with regular expressions. You'd need to add a third party dep like JSoup, use that to parse this HTML and change it.
Furthermore, CSS parsing is not exactly trivial either, so on top of this you'd need a CSS parser.
Really, you need to go back to the drawing board. You have systems in place that ended up in 'I need to parse any HTML for CSS in style attributes and modify those', and the solution to that problem lies further up the chain. Uses classes instead of style attributes, or find the place that makes these style attributes and fix it there.
It's always this exact form
This will fail horribly unless, your input is exactly like this:
- a span with a style and nothing else, with plain text content.
- Optionally, any number of those spans, but not nested.
- If style is present, then via
style=, and all style keys necessarily end in a semi-colon, even though that is optional in HTML.
- The font size is always specified using the exact spelling
font-size.
Well, as long as you take great care to write down someplace that the HTML you input into this algorithm is restricted to remain that simple, you could in theory do this with regular expressions, but be aware that any fancying up of this HTML is going to break this, and the only true answer, capable of dealing with future changes, remains JSoup!
input.replaceAll("font-size\\s*:(.*?);", "font-size:$1 !important;");
will do the job.
NB: If font-size appears as text inside the spans, that's bad. You'd have to extend your regex to look for e.g. too, but strings are hard to parse with regexes either (specifically, trying to dance around backslash-escaped quotes is tricky). This all goes back to: You have found yourself in a nasty place; you really should fix this elsewhere in the chain.