Only strip commas surrounded by numbers in a string

Posted by overlook77 on Stack Overflow See other posts from Stack Overflow or by overlook77
Published on 2013-06-26T16:14:43Z Indexed on 2013/06/26 16:21 UTC
Read the original article Hit count: 141

Filed under:
|
|

I am using StreamReader to read each line of a CSV file into a string. As I am processing each line, I need to strip out any commas that ONLY are surrounded by other numbers.

For example, if the string was:

"textfield1", "textfield2", "100.00", "1,070.00"

I would need to only take the comma out of "1,070.00" from this entire string, resulting in:

"textfield1", "textfield2", "100.00", "1070.00"

Each string read from the CSV file can differ in number of fields, length, etc., so I need to use something (Regular Expressions maybe?) that looks at the entire string without hardcoding a location or blanket removal of all commas.

Here is the approach I have been trying:

StreamReader sr = new StreamReader(strInputFile);
string nextLine = sr.ReadLine();     

try
{
    while ((nextLine = sr.ReadLine()) != null)
    {
        string rawtext = nextLine.Replace("[0-9]"+","+"[0-9]" , "[0-9]"+"[0-9]");

        // ....rest of code
    }
}

This obviously doesn't work because I don't understand how to do this :)
I am new at C# and inexperienced in Regex, so hopefully this is relatively simple.

© Stack Overflow or respective owner

Related posts about c#

Related posts about csv