Currency values string split by comma

前端 未结 3 1617
情书的邮戳
情书的邮戳 2021-01-18 11:57

I have a String which contains formatted currency values like 45,890.00 and multiple values seperated by comma like 45,890.00,12,345.00,23,765.34,56,908.5

3条回答
  •  情书的邮戳
    2021-01-18 12:29

    You need a regex "look behind" (?<=regex), which matches, but does consume:

    String regEx = "(?<=\\.[0-9]{2}),";
    

    Here's your test case now working:

    public static void main(String[] args) {
        String currencyValues = "45,890.00,12,345.00,23,765.34,56,908.50";
        String regEx = "(?<=\\.[0-9]{2}),"; // Using the regex with the look-behind
        String[] results = currencyValues.split(regEx);
        for (String res : results) {
            System.out.println(res);
        }
    }
    

    Output:

    45,890.00
    12,345.00
    23,765.34
    56,908.50
    

提交回复
热议问题