Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

regex

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.

like image 362
Thomas Grady Avatar asked Feb 26 '14 16:02

Thomas Grady


2 Answers

Notice the difference between the commas you want to remove and the ones you don't. The field seprator is always followed by a space, so use the following regex:

String.replaceAll(",[^ ]", "");

The regex ,[^ ] means "comma, followed by something that is not a space". To replace the dollar sign, you should probably escape it since it has special meaning for regex:

String.replaceAll("\\$", "");
like image 60
Mad Physicist Avatar answered Sep 28 '22 04:09

Mad Physicist


You can use:

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

//=> fieldA, fieldB, fieldC, 1000.00, fieldD, FieldE
like image 25
anubhava Avatar answered Sep 28 '22 04:09

anubhava