java 从字符串中删除前导/尾随零的正则表达式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15019080/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Regular expression to remove leading/trailing zeros from a string
提问by Sam
Is this the proper REGEX to remove trailing decimal and zeroes from a string? I can't get it to work. What am I missing?
这是从字符串中删除尾随小数和零的正确正则表达式吗?我无法让它工作。我错过了什么?
78.80 -> 78.8
78.80 -> 78.8
str.replaceAll("^.0*$", "");
i need only 2 decimal points as well like 78.008 should be 78.01
我只需要 2 个小数点,就像 78.008 应该是 78.01
and if it is 78.10 the 78.1 only.
如果是 78.10,则只有 78.1。
回答by xanatos
No, the correct regular expression is something more complex, and it needs positive look-behind.
不,正确的正则表达式更复杂,它需要积极的回顾。
str = str.replaceAll("\.0*$|(?<=\.[0-9]{0,2147483646})0*$", "");
you have to escape the .
, because otherwise it means "any character", you have to anchor the regex only to the end of the string, and you have to say that the 0
digits must be after a .
plus some optional non-zero digits.
你必须转义.
,否则它意味着“任何字符”,你必须将正则表达式锚定到字符串的末尾,并且你必须说0
数字必须在.
加上一些可选的非零数字之后。
Addendum: there is a "special case" that should be handled: 1.00
. We handle it by using the |
. The first sub-expression means "a dot plus only zeroes" and matches even the dot (that in this way is deleted)
附录:有一个“特殊情况”应当处理:1.00
。我们通过使用|
. 第一个子表达式的意思是“一个点加上只有零”,甚至匹配这个点(以这种方式被删除)
And remember that in Java strings are immutable, so replaceAll
will create a new string.
请记住,在 Java 中字符串是不可变的,因此replaceAll
会创建一个新字符串。
Note the use of {0,2147483646}
: if you used a *
you would get a Look-behind group does not have an obvious maximum length
, because the *
would be converted to {0,2147483647}
and considering the "+1" length of the \\.
it would overflow, so we put the maximum number of digits possible (2147483647, maximum value of a signed int) minus 1 for the dot.
注意使用{0,2147483646}
: 如果你使用 a*
你会得到 a Look-behind group does not have an obvious maximum length
,因为*
将被转换为{0,2147483647}
并考虑到它的“+1”长度\\.
会溢出,所以我们把可能的最大位数(2147483647,a的最大值有符号 int) 减 1 表示点。
Test example: http://ideone.com/0NDTSq
测试示例:http: //ideone.com/0NDTSq
回答by yiannis
Since you want rounding of the numbers, you should not use regular expressions. You should use DecimalFormat
:
由于您想要对数字进行四舍五入,因此不应使用正则表达式。你应该使用DecimalFormat
:
DecimalFormat twoDForm = new DecimalFormat("#.#");
Double d = Double.parseDouble("123.078");
String s = twoDForm.format(d);
System.out.println(s);
will output:
将输出:
123.08
回答by stema
With the anchors ^
and $
you are trying to match the whole string and replace it. And this will not match since there is more in the string.
使用锚点^
,$
您正在尝试匹配整个字符串并替换它。这将不匹配,因为字符串中有更多内容。
Try this
试试这个
str.replace("0*$", "");
With 0*$
you will match only 0 or more "0" at the end of the string.
随着0*$
你只会0或多个“0”的字符串的结尾相匹配。
Note:This will also change "1000" to "1"!
注意:这也会将“1000”更改为“1”!