货币值字符串用逗号分隔

我有一个字符串,其中包含格式货币值(如)45,890.00和多个值(以逗号分隔)45,890.00,12,345.00,23,765.34,56,908.50

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

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),它匹配但确实消耗:

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

这是您现在可以使用的测试用例:

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

以上是 货币值字符串用逗号分隔 的全部内容, 来源链接: utcz.com/qa/405494.html

回到顶部