移除所有点号,但不要移除数字中的点号 – Java

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

Remove all the dots but not \in numbers - Java

问题

我正在尝试替换字符串中除了像 1.02 这样的数字之外的所有 .

我有一个字符串:-

  1. String rM = "提供了51.3升水。使用了23.3升。"

如果我使用 rM.replaceAll(),那么所有的句点都会被替换,我希望我的字符串是:-

  1. 51.3升水已被提供 23.3升已被使用

在Java中是否有可能实现?

英文:

I am trying to replace all the . in a string except numbers like 1.02

I have a string : -

  1. String rM = "51.3L of water is provided. 23.3L is used."

If I use rM.replaceAll() then every dot will be replaced, I want my string to be : -

  1. 51.3L of water is provided 23.3L is used

Is it possible to do in java?

答案1

得分: 3

我不是Java开发人员,但你可以尝试以下类似的模式。

  1. rM = rM.replaceAll("(?<=[a-z\\s])\\.", "");
英文:

I am not a java developer but can you try it with a pattern like below.

  1. rM = rM.replaceAll(&quot;(?&lt;=[a-z\\s])\\.&quot;, &quot;&quot;);

答案2

得分: 1

是的,这是可能的。类似以下的代码应该可以工作。正则表达式应该只检查element是否以字符0-9开始。如果是,不要改变这个元素。如果不是,将任何 . 替换为空字符串。

  1. String rM = "51.3L的水已提供。使用了23.3L。";
  2. String[] tokens = rM.split(" ");
  3. StringBuffer buffer = new StringBuffer();
  4. for (String element : tokens) {
  5. if (element.matches("[0-9]+.*")) {
  6. buffer.append(element + " ");
  7. } else {
  8. buffer.append(element.replace(".", "") + " ");
  9. }
  10. }
  11. System.out.println(buffer.toString());

输出:

  1. 51.3L的水已提供使用了23.3L
英文:

yes its possible. Something like the following should work. The regex should just check that the element starts with a character 0-9. If yes, don't change the element. If no, replace any . with the empty string.

  1. String rM = &quot;51.3L of water is provided. 23.3L is used.&quot;;
  2. String[] tokens = rM.split(&quot; &quot;);
  3. StringBuffer buffer = new StringBuffer();
  4. for (String element : tokens) {
  5. if (element.matches(&quot;[0-9]+.*&quot;)) {
  6. buffer.append(element + &quot; &quot;);
  7. } else {
  8. buffer.append(element.replace(&quot;.&quot;, &quot;&quot;) + &quot; &quot;);
  9. }
  10. }
  11. System.out.println(buffer.toString());

Output:

  1. 51.3L of water is provided 23.3L is used

答案3

得分: 1

这是一个简单的方法,假设您想要去掉紧跟在非空白字符后面的点。

以下代码基本上通过空格将句子拆分为单词,并删除每个结果字符序列中的尾随点,然后将它们重新连接成单个字符串。

  1. public static void main(String[] args) {
  2. // 示例句子
  3. String rM = "51.3L of water is provided. 23.3L is used.";
  4. // 通过空格拆分句子
  5. String[] parts = rM.split("\\s+");
  6. // 遍历所有部分
  7. for (int i = 0; i < parts.length; i++) {
  8. // 检查部分是否以点结尾
  9. if (parts[i].endsWith(".")) {
  10. // 如果是,将该部分替换为去掉末尾点的部分
  11. parts[i] = parts[i].substring(0, parts[i].length() - 1);
  12. }
  13. }
  14. // 将部分重新连接为句子字符串
  15. String removedUndesiredDots = String.join(" ", parts);
  16. // 并打印输出
  17. System.out.println(removedUndesiredDots);
  18. }

输出为

  1. 51.3L of water is provided 23.3L is used
英文:

Here's a simple approach that assumes you want to get rid of dots that are placed directly after a char which isn't a whitespace.

The following code basically splits the sentence by whitespace(s) and removes trailing dots in every resulting character sequence and joins them afterwards to a single String again.

  1. public static void main(String[] args) {
  2. // example sentence
  3. String rM = &quot;51.3L of water is provided. 23.3L is used.&quot;;
  4. // split the sentence by whitespace(s)
  5. String[] parts = rM.split(&quot;\\s+&quot;);
  6. // go through all the parts
  7. for (int i = 0; i &lt; parts.length; i++) {
  8. // check if one of the parts ends with a dot
  9. if (parts[i].endsWith(&quot;.&quot;)) {
  10. // if it does, replace that part by itself minus the trailing dot
  11. parts[i] = parts[i].substring(0, parts[i].length() - 1);
  12. }
  13. }
  14. // join the parts to a sentence String again
  15. String removedUndesiredDots = String.join(&quot; &quot;, parts);
  16. // and print that
  17. System.out.println(removedUndesiredDots);
  18. }

The output is

  1. 51.3L of water is provided 23.3L is used

答案4

得分: 1

replaceAll()方法与正确的正则表达式可以为您完成此操作。

这里使用了负向先行断言和负向后行断言来查找不在十进制数中间的'.'。

  1. rM.replaceAll("(?<![\\d])\\.(?![\\d]+)", "")
英文:

replaceAll() with the right regex can do it for you.

This uses a negative look-ahead and look-behind to look for a '.' not in the middle of a decimal number.

  1. rM.replaceAll(&quot;(?&lt;![\\d])\\.(?![\\d]+)&quot;, &quot;&quot;)

答案5

得分: 1

使用负向先行断言,您可以使用\.(?![\d](\.[\d])?)

  1. private static final String DOTS_NO_NUM_REGEX = "\\.(?![\\d](\\.[\\d])?)";
  2. private static final Pattern PATTERN = Pattern.compile(DOTS_NO_NUM_REGEX);
  3. public static void main(String[] args) {
  4. String s = "51.3L of water is provided. 23.3L is used.";
  5. String replaced = PATTERN.matcher(s).replaceAll("");
  6. System.out.println(replaced);
  7. }

输出:

  1. 51.3L of water is provided 23.3L is used
英文:

Using negative lookahead you can use \.(?![\d](\.[\d])?).

  1. private static final String DOTS_NO_NUM_REGEX = &quot;\\.(?![\\d](\\.[\\d])?)&quot;;
  2. private static final Pattern PATTERN = Pattern.compile(DOTS_NO_NUM_REGEX);
  3. public static void main(String[] args)
  4. {
  5. String s = &quot;51.3L of water is provided. 23.3L is used.&quot;;
  6. String replaced = PATTERN.matcher(s).replaceAll(&quot;&quot;);
  7. System.out.println(replaced);
  8. }

Output:

  1. 51.3L of water is provided 23.3L is used

huangapple
  • 本文由 发表于 2020年10月16日 18:19:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/64387285.html
匿名

发表评论

匿名网友

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

确定