tags:

views:

137

answers:

2

In an application I'm developing, someone thought it would be ok idea to include commas in the values of a csv file.

So what I'm trying to do select these values and then strip the commas out of these values. But the Regex I've built won't match to anything.

The Regex pattern is: .*,\"<money>(.+,.+)\",?.*

And the sort of values I'm trying to match would be the 2700, 2650 and 2600 in "2,700","2,650","2,600".

Thanks in advance for any help you can give.

+1  A: 

Commas are allowed in CSV's and should not cause a problem if you use a text qualifier (usually double quote ").

Here are details: http://en.wikipedia.org/wiki/Comma-separated_values

On to the regex:

This code works for your sample data (but only allows one comma, basically thousands seperated numbers up to 999,999):

string ResultString = null;
try {
    ResultString = Regex.Replace(myString, "([0-9]{1,3})(?:(,)?([0-9]{3})?)", "$1$3");
} catch (ArgumentException ex) {
    // Syntax error in the regular expression
}

It takes this:

Test 12,205 26,000 Test. And the sort of values I'm trying to match would be the 2700, 2650 and 2600 in "2,700","2,650","2,600" Thanks in advance for any help you can give.

and produces this:

Test 12205 26000 Test. And the sort of values I'm trying to match would be the 2700 2650 and 2600 in "2700","2650","2600" Thanks in advance for any help you can give.

Ryan Cook
A: 

Thanks for the answer Ryan. I should've kept my temper in check a bit more. Though still, commas in a CSV that don't seperate values?

After asking around the office I got pointed towards a free Regex designer application that I used to build the pattern I needed. The application is Rad Software Regular Expression Designer

Oh and for the answer purposes the pattern I came out with is: \"(?<money>[0-9,.$]*)\"

Edit: Final Andwer

Woah. Completely forgot about this question. I played around with the regex even more and came out with one that would match everything I needed. The regex is:

\"([0-9.$]+(,[0-9.]+)+)\"

That regex has been able to match any deciaml string within a double quote that I've thrown at it.

Twisted Mentat