Java regex for remove no digit chracter except decimal point

為{幸葍}努か 提交于 2021-02-05 09:44:43

问题


I have following strings:

S/. 05.56
S/. 0.0
S/. 0.00
S/. 90.10
S/.   01.23
S/.   1.00   
S/.1.80
$/.9.80
$/.  10.80
$/.    89.81
$    89.81

I need this output

05.56
0.0
0.00
90.10
01.23
1.00
1.80
9.80
10.80
89.81

I need to remove all non-digit character before first number, Then regex haven't to remove decimal point

I tried this regex but doesn't work:

e.replaceAll("[a-zA-z](/.)( )*", ""); //don't remove $
e.replaceAll("[^0-9.]", ""); //This show .3.4 I need remove the first point decimal

I need to remove all monetary symbol

Thanks for help!


回答1:


Use ^\\D+ to replace all non-digits from the beginning of each string.

public static void main(String[] args) {

    String[] str = { "S/. 05.56", "S/. 0.0", "S/. 0.00", "S/. 90.10", "S/.   01.23", "S/.   1.00", "S/.1.80",
            "$/.9.80", "$/.  10.80", "$/.    89.81", "$    89.81" };

    for (String s : str) {
        System.out.println(s.replaceAll("^\\D+", ""));
    }

}

O/P :

05.56
0.0
0.00
90.10
01.23
1.00
1.80
9.80
10.80
89.81
89.81


来源:https://stackoverflow.com/questions/39396239/java-regex-for-remove-no-digit-chracter-except-decimal-point

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!