Using regex in java to remove a dollar sign and comma from a currency field within a field in a column delimited file

java, regex

Solution

You can use:

String repl = str.replaceAll("(?<=\\d),(?=\\d)|\\$", "");

//=> fieldA, fieldB, fieldC, 1000.00, fieldD, FieldE

Problem

I am having a bit of a challenge trying to remove the dollar sign and thousands separator (comma in my case) from a currency field that resides on a record in a comma delimited file using the string.replaceAll command. Removing the dollar sign and comma (all commas) is no problem, but the comma field delimiters need to stay and here lies my challenge. I have spend a bit of time researching this and am spinning my wheels. Below is a pattern for the type of record being read in and what I would like to look like after. Before - `fieldA, fieldB, fieldC, $1,000.00, fieldD, FieldE` After - `fieldA, fieldB, fieldC, 1000.00, fieldD, fieldE` Or would I be better off not using a regex pattern? Any help is greatly appreciated.

Original source