1

How to append a string with dynamic data

I have an HTML string, want to add !important-tag in a font-size element.

Conditions:

  1. The font size is dynamic value - like 36pt, 24pt, 14pt - Its not constant.
  2. There is no order span tags. It's not coming regular span order format.

I have tried the regexp and replace method but it's not working. Please help me.

Input String

String htmlData = "<span style=\"font-weight: bold;\">First</span><span style=\"font-size: 36pt;\">Second</span><span style=\"font-family: Arial;\">third</span>";

Output

!important added in font-size element

String htmlData = "<span style=\"font-weight: bold;\">First</span><span style=\"font-size: 36pt;!important\">Second</span><span style=\"font-family: Arial;\">third</span>";

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

You can try using replaceAll:

String html = "<span style=\"font-weight: bold;\">First</span><span style=\"font-size: 36pt;\">Second</span><span style=\"font-family: Arial;\">third</span>";

String replaced = html.replaceAll("font-size: ([0-9]*)pt;", "font-size: $1pt !important;");
System.out.println(replaced);

Comments

1

i think you can create a string such as

"font-size: "+value+"pt;" and in your html string, you can simply find and replace this string with "font-size: "+value+"pt;!important" by using htmlData.replaceFirst(string1, string2);

Comments

0

It is better to convert your string to html, and to set the important option (this solution let you change every thing you want in the html and css) But if you only need to do this specific change "!important" then you need to chose the easy way with :

input.replaceAll

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.