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

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

Replace .(dot) inside number Java between two elements

问题

我有这个字符串:

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

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

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

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

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

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

谢谢

英文:

I have this String:

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:

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

I cant figure it out, i have this:

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

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

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

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

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

You may use capturing groups + escape the /:

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

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

答案2

得分: 1

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

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

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

正则表达式解释:

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

演示:

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

Java 代码示例:

public static void main(String args[]) {
   String regex = "(?<=\\d)\\.(?=\\d)";
  
   String str = "<p>23.5</p>";
   String str2 = "Mr. John <p>23.5</p> Hello";
   String str3 = "Mr. John <p>23.5</p> Hello 12.2324";

   System.out.println(str.replaceAll(regex, ","));   // <p>23,5</p>
   System.out.println(str2.replaceAll(regex, ","));  // Mr. John <p>23,5</p> Hello
   System.out.println(str3.replaceAll(regex, ","));  // Mr. John <p>23,5</p> Hello 12,2324
}
英文:

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

(?&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:

public static void main(String args[]) {
   String regex = &quot;(?&lt;=\\d)\\.(?=\\d)&quot;;
  
   String str = &quot;&lt;p&gt;23.5&lt;/p&gt;&quot;;
   String str2 = &quot;Mr. John &lt;p&gt;23.5&lt;/p&gt; Hello&quot;;
   String str3 = &quot;Mr. John &lt;p&gt;23.5&lt;/p&gt; Hello 12.2324&quot;;

   System.out.println(str.replaceAll(regex, &quot;,&quot;));   // &lt;p&gt;23,5&lt;/p&gt;
   System.out.println(str2.replaceAll(regex, &quot;,&quot;));  // Mr. John &lt;p&gt;23,5&lt;/p&gt; Hello
   System.out.println(str3.replaceAll(regex, &quot;,&quot;));  // Mr. John &lt;p&gt;23,5&lt;/p&gt; Hello 12,2324
}

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:

确定