货币值字符串由逗号分隔 [英] Currency values string split by comma

查看:130
本文介绍了货币值字符串由逗号分隔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串,其中包含格式化的货币值,例如 45,890.00 ,并且逗号分隔的多个值如 45,890.00,12,345.00,23,765.34,56,908.50 ..

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.50 ..

我想提取并处理所有货币值,但无法找出正确的正则表达式,这就是我试过的

I want to extract and process all the currency values, but could not figure out the correct regular expression for this, This is what I have tried

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}[,]";
    String[] results = currencyValues.split(regEx);
    //System.out.println(Arrays.toString(results));
    for(String res : results) {
        System.out.println(res);
    }
}

这个输出是:

45,890 //removing the decimals as the reg ex is exclusive
12,345
23,765
56,908.50

有人可以帮我这个吗?

推荐答案

你需要一个正则表达式看后面(?< = regex),它匹配,但消耗:

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);
    }
}

输出:

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

这篇关于货币值字符串由逗号分隔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆