如何在Java中使用动态数据追加字符串

huangapple go评论64阅读模式
英文:

How to append string with dynamic data in Java

问题

如何在动态数据中添加字符串

我有一个HTML字符串,想在一个字号元素中添加`!important`标签。

条件:

1. 字号是动态值 - 像36pt、24pt、14pt - 它不是固定的。
2. 没有顺序的span标签。它们的出现顺序不是固定的。

我尝试了正则表达式和替换方法,但不起作用。请帮助我。

输入字符串:

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

输出:

**在字号元素中添加了!important**

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

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>";

答案1

得分: 1

这取决于输入字符串的实际情况。您的“conditions”(条件)列表不足够。

任何 HTML,实际上

好的,那么,您需要更多关于需要更改哪种字号样式的信息。所有的样式,文档中的任何位置?就像“获取输入的 HTML,查找任何地方具有style属性的所有span,然后解析这些样式属性以查找任何font-size CSS键,并在其中添加!important一样。

然后答案就会变得非常非常棘手。HTML 不是规则的,因此无法使用正则表达式进行解析。您需要添加第三方依赖,例如 JSoup,使用它来解析此 HTML 并进行更改。

此外,CSS 解析也不是完全简单的,因此除此之外,您还需要一个 CSS 解析器。

实际上,您需要重新考虑。您已经在“我需要解析任何 HTML 以获取 style 属性中的 CSS 并对其进行修改”这个问题上找到了解决方案,而该问题的解决方案则在更高的层次上。请改用类而不是样式属性,或者找到生成这些样式属性的地方,并在那里进行修复。

它总是这个确切的形式

除非您的输入完全符合以下情况,否则这将会“失败得非常可怕”:

  • 带有样式但没有其他内容的span,具有纯文本内容。
  • 可选地,任意数量的这些span,但不能嵌套。
  • 如果存在样式,则通过style=,并且所有样式键必须以分号结尾,尽管在 HTML 中这是可选的。
  • 字号始终使用精确的拼写font-size进行指定。

只要您非常小心地在某个地方写下,将输入到此算法中的 HTML 限制为保持如此简单,您理论上可以使用正则表达式来执行此操作,但要注意,对此 HTML 进行任何增强都将会破坏它,而唯一真正的、能够处理未来更改的答案仍然是 JSoup!

input.replaceAll("font-size\\s*:(.*?);", "font-size:$1 !important;");

将会完成工作。

注:如果font-size作为文本出现在span内部,那就不好。您必须扩展您的正则表达式,以查找例如<span>,但是使用正则表达式解析字符串也很困难(具体而言,尝试在转义引号周围进行处理是棘手的)。所有这些都追溯到:您发现自己处于一个糟糕的位置;您真的应该在链条的其他地方修复这个问题。

英文:

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(&quot;font-size\\s*:(.*?);&quot;, &quot;font-size:$1 !important;&quot;);

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. <span> 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.

答案2

得分: 1

你可以尝试使用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);
英文:

You can try using replaceAll:

String html = &quot;&lt;span style=\&quot;font-weight: bold;\&quot;&gt;First&lt;/span&gt;&lt;span style=\&quot;font-size: 36pt;\&quot;&gt;Second&lt;/span&gt;&lt;span style=\&quot;font-family: Arial;\&quot;&gt;third&lt;/span&gt;&quot;;

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

答案3

得分: 1

我认为您可以创建一个字符串,例如

&quot;font-size: &quot;+value+&quot;pt;&quot;,在您的 HTML 字符串中,您可以通过使用 htmlData.replaceFirst(string1, string2); 来查找并替换此字符串为 &quot;font-size: &quot;+value+&quot;pt;!important&quot;

英文:

i think you can create a string such as

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

答案4

得分: 0

最好将您的字符串转换为HTML,并设置重要选项(此解决方案使您可以在HTML和CSS中更改所需的任何内容)

但是,如果您只需要进行特定的“!important”更改,则可以选择简单的方式:

input.replaceAll
英文:

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

huangapple
  • 本文由 发表于 2020年10月6日 21:35:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/64226976.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定