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

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.

Answers


You can use:

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

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

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("\\$", "");

try this

s = s.replaceAll("\\$|(?<=\\d),(?=\\d)", "");

Need Your Help

3D imageView rotation by 360 degrees by user touch

iphone ios objective-c xcode ios5

We have a bike image. When a user swipes the image, we have to show the image in different angles.We have 60 images in a different angle for that bike,when user swipes left or right on

How to get dtmf value in dailpan

php sip asterisk

I have one dialplan in which what i want is,if user press any key then play file again but i can not understand how to get dtmf value in dialplan.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.