java 如何仅根据数字、点和逗号过滤字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5979640/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 13:49:18  来源:igfitidea点击:

How do I filter a string on just numbers, dots and commas?

javaregex

提问by truy37

Okay, so ive got a long string, and I want to remove everything thats inside, except for decimal numbers, comma's and dots,

好的,所以我有一个很长的字符串,我想删除里面的所有内容,除了十进制数字、逗号和点,

I have tried:

我努力了:

str = str.replace("[^0-9\.\,]","");

But this just ends up in nothing..

但这最终一无所获..

Can anyone help me?

谁能帮我?

Thanks!

谢谢!

回答by jjnguy

You don't need to escape the characters in the character group. You should also be using replaceAll().

您不需要对字符组中的字符进行转义。您还应该使用replaceAll().

str = str.replaceAll("[^0-9.,]+","");

回答by Martijn

Try str.replaceAll("[^0-9.,]+","");

尝试 str.replaceAll("[^0-9.,]+","");