在两个元素之间替换Java中数字内部的点。

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

Replace .(dot) inside number Java between two elements

问题

我有这个字符串:

  1. String str = "<p>23.5</p>";

我想仅在<p>元素内部将点替换为逗号。我需要的输出是:

  1. &lt;p&gt;23,5&lt;/p&gt;

我无法弄清楚,我有以下代码:

  1. str = str.replaceAll("&quot;(?&lt;=&lt;p&gt;)\\.(?=&lt;/p&gt;)&quot;", "&quot;,&quot;");

但它不起作用。我需要仅在特定标签(这是一个字符串中的 XML)的元素中替换点,这里是<p>。

谢谢

英文:

I have this String:

  1. String str = &quot;&lt;p&gt;23.5&lt;/p&gt;&quot;;

And i want to replace the dot for comma only inside <p> elements. The output i need is:

  1. &lt;p&gt;23,5&lt;/p&gt;

I cant figure it out, i have this:

  1. str = str.replaceAll(&quot;(?&lt;=&lt;p&gt;)\\.(?=&lt;/p&gt;)&quot;, &quot;,&quot;);

But it doesnt work. I need to replace dot only in elements with particular tag (is an xml in a String), in this case <p>.

Thank you

答案1

得分: 1

你可以使用捕获组+转义斜杠:

  1. str = str.replaceAll("(?<=<p>)(\\d*)\\.(\\d+)(?=<\\/p>)", "$1,$2");

如果你想要替换所有数字中的点号,同样可以使用:

  1. str = str.replaceAll("(\\d*)\\.(\\d+)", "$1,$2");
英文:

You may use capturing groups + escape the /:

  1. str = str.replaceAll(&quot;(?&lt;=&lt;p&gt;)(\\d*)\\.(\\d+)(?=&lt;\\/p&gt;)&quot;, &quot;$1,$2&quot;);

If you want to replace dot in all numbers, you may just as well use

  1. str = str.replaceAll(&quot;(\\d*)\\.(\\d+)&quot;, &quot;$1,$2&quot;);

答案2

得分: 1

以下是您要求的翻译内容:

以下正则表达式将匹配位于数字字符之间的点字符:

  1. (?<=\d)\.(?=\d)

正则表达式解释:

  • \d - 匹配任何 0-9 之间的数字
  • (?<=\d)\. - 正向后查找,匹配在点号之前有一个数字的任何点号字符
  • \.(?=\d) - 正向前查找,匹配在点号之后有一个数字的任何点号字符

演示:

https://regex101.com/r/WMEjPl/1

Java 代码示例:

  1. public static void main(String args[]) {
  2. String regex = "(?<=\\d)\\.(?=\\d)";
  3. String str = "<p>23.5</p>";
  4. String str2 = "Mr. John <p>23.5</p> Hello";
  5. String str3 = "Mr. John <p>23.5</p> Hello 12.2324";
  6. System.out.println(str.replaceAll(regex, ",")); // <p>23,5</p>
  7. System.out.println(str2.replaceAll(regex, ",")); // Mr. John <p>23,5</p> Hello
  8. System.out.println(str3.replaceAll(regex, ",")); // Mr. John <p>23,5</p> Hello 12,2324
  9. }
英文:

Following regex will match the dot character that is between numerical characters

  1. (?&lt;=\d)\.(?=\d)

Regex Explanation:

  • \d - match any digit between 0-9
  • (?&lt;=\d)\. - positive look-behind to match any . character that has a digit just before it
  • \.(?=\d) - positive look-ahead to match any . character that has a digit just after it

Demo:

https://regex101.com/r/WMEjPl/1

Java Code Example:

  1. public static void main(String args[]) {
  2. String regex = &quot;(?&lt;=\\d)\\.(?=\\d)&quot;;
  3. String str = &quot;&lt;p&gt;23.5&lt;/p&gt;&quot;;
  4. String str2 = &quot;Mr. John &lt;p&gt;23.5&lt;/p&gt; Hello&quot;;
  5. String str3 = &quot;Mr. John &lt;p&gt;23.5&lt;/p&gt; Hello 12.2324&quot;;
  6. System.out.println(str.replaceAll(regex, &quot;,&quot;)); // &lt;p&gt;23,5&lt;/p&gt;
  7. System.out.println(str2.replaceAll(regex, &quot;,&quot;)); // Mr. John &lt;p&gt;23,5&lt;/p&gt; Hello
  8. System.out.println(str3.replaceAll(regex, &quot;,&quot;)); // Mr. John &lt;p&gt;23,5&lt;/p&gt; Hello 12,2324
  9. }

huangapple
  • 本文由 发表于 2020年7月24日 19:30:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/63072673.html
匿名

发表评论

匿名网友

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

确定