在Java中,仅在字符串中的最后一位是一个单独的数字时删除它。

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

Remove last digit in a string in Java only if it is just one digit present

问题

Here's the translation:

我想要在Java字符串中删除最后一个数字,只有当只有一个数字时才删除。

例如:

String text = "I am the9 number1"; // 应为 "I am the number"

不需要执行任何操作的情况:

String = "I913 am the55 number11"; // 保持不变。

我尝试了以下方法,但没有成功:

String text = "I am the1 number1";
text = text.replaceAll("^[0-9]$", "");

没有起作用。需要帮助吗?

英文:

I would like to remove last digit in a Java string, only if it is just one digit present.

E.g.

String text = "I am the9 number1"; // should be "I am the number"

Cases where it should do nothing:

String = "I913 am the55 number11"; // Should remain the same.

I tried this without success:

String text = "I am the1 number1";
text = text.replaceAll("^[0-9]$", "");

Didnt work. Any help?

答案1

得分: 1

要替换字符串中的最后一个数字,您可以使用以下代码:

text = text.replaceAll("B(?<!\\d)\\d\\b", "");

请参阅[正则表达式演示][1]。

**详细信息**

- `\B` - 必须在当前位置的左侧立即出现字母或下划线。
- `(?<!\d)` - 负向回顾先行,如果当前位置的左侧有数字,则匹配失败。
- `\d` - 数字
- `\b` - 单词边界。

[Java演示][2]:

String rx = "\\B(?<!\\d)\\d\\b";
System.out.println("I am the number1".replaceAll(rx, ""));
// => I am the number
System.out.println("I am the number11".replaceAll(rx, ""));
// => I am the number11
System.out.println("I am 4the number1 @#@%$grtuy".replaceAll(rx, ""));
// => I am 4the number @#@%$grtuy
System.out.println("I am number1 1 and you number2 2".replaceAll(rx, ""));
// => I am number 1 and you number 2

[1]: https://regex101.com/r/D3y94p/3
[2]: https://ideone.com/MXtKFO
英文:

To replace the last digit in a string you may use

text = text.replaceAll(&quot;\\B(?&lt;!\\d)\\d\\b&quot;, &quot;&quot;);

See the regex demo.

Details

  • \B - a letter or _ must appear immediately to the left of the current position
  • (?&lt;!\d) - a negative lookbehind that fails the match if there is a digit immediately to the left of the current location
  • \d - a digit
  • \b - a word boundary.

Java demo:

String rx = &quot;\\B(?&lt;!\\d)\\d\\b&quot;;
System.out.println(&quot;I am the number1&quot;.replaceAll(rx, &quot;&quot;));
// =&gt; I am the number
System.out.println(&quot;I am the number11&quot;.replaceAll(rx, &quot;&quot;)); 
// =&gt; I am the number11
System.out.println(&quot;I am 4the number1 @#@%$grtuy&quot;.replaceAll(rx, &quot;&quot;)); 
// =&gt; I am 4the number @#@%$grtuy
System.out.println(&quot;I am number1 1 and you number2 2&quot;.replaceAll(rx, &quot;&quot;));
// =&gt; I am number 1 and you number 2

答案2

得分: 1

你可以使用

(?<=[A-Za-z])\d(?!\\d)

正则表达式演示

详情

(?<=[A-Za-z]):表示匹配数字前面是单词字符的正向后查找。

\d:表示数字。

(?!\\d):表示匹配的数字后面不能再有其他数字的负向前瞻。

示例代码

text = text.replaceAll("(?<=[A-Za-z])\\d(?!\\d)", "");

英文:

You can use

(?&lt;=[A-Za-z])\d(?!\d)

Regex Demo

Details

(?&lt;=[A-Za-z]): it indicates word character before digit as positive lookbehind

\d: indicates number

(?!\d): there has to be no other number after matched number (negative lookahead)

Sample Code

text = text.replaceAll(&quot;(?&lt;=[A-Za-z])\d(?!\d)&quot;, &quot;&quot;);

huangapple
  • 本文由 发表于 2020年8月1日 01:53:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/63196787.html
匿名

发表评论

匿名网友

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

确定