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);
}
}
The output of this is:
45,890 //removing the decimals as the reg ex is exclusive
12,345
23,765
56,908.50
Could someone please help me with this one?
You need a regex “look behind”
(?<=regex), which matches, but does consume:Here’s your test case now working:
Output: