How to remove quotes, commas, from numbers (considered as strings)?

  • Thread starter Thread starter sherifffruitfly
  • Start date Start date
S

sherifffruitfly

Hi all,

I've got a csv file for numeric data, some of which are greater than
10^3. Some bright fellow trying to br helpful put US-standard commas in
these numbers, and to maintain the correct cell-index put quotation
marks around the comma'd result.

Example csv line:

43.56,345.2,"1,285,100",45.6

I would like to replace the quoted-comma'd numbers with their
unquoted-uncomma'd versions. I'm reading the csv line-by-line, putting
a line into a string called csvLine. I'd like to call the static
Regex.Replace method, in a way such as the following:

Regex.Replace(csvLine, quotedCommadNumberPattern,
noQuotesNoCommasPrevArg)

I can come up with the regular expression for the second argument. But
I'm not sure about the third. In particular, how can I make a regular
expression refer to "the digits in what was found in the previous
argument"?

Or am I going about this all wrong?

Thanks for any ideas,

cdj
 
This should work:

string s1 = "43.56,345.2,\"1,285,100\",45.6";
string[] sa1 = SplitQuoted(s1, ",");
for (int i = 0; i < sa1.Length; i++)
sa1 = sa1.Replace(",", "");
s1 = string.Join(",", sa1);
Console.WriteLine(s1);


public static string[] SplitQuoted(string text, string delimiters)
{
// Default delimiters are a space and tab (e.g. " \t").
// All delimiters not inside quote pair are ignored.
// Default quotes pair is two double quotes ( e.g. '""' ).
if (text == null)
throw new ArgumentNullException("text", "text is null.");
if (delimiters == null || delimiters.Length < 1)
delimiters = " \t"; // Use space and tab as default
delimiters.

ArrayList res = new ArrayList();

// Build the pattern that searches for both quoted and unquoted
elements
// notice that the quoted element is defined by group #2 (g1)
// and the unquoted element is defined by group #3 (g2).

string pattern =
@"""([^""\\]*[\\.[^""\\]*]*)""" +
"|" +
@"([^" + delimiters + @"]+)";

// Search the string.
foreach (System.Text.RegularExpressions.Match m in
System.Text.RegularExpressions.Regex.Matches(text, pattern))
{
string g0 = m.Groups[0].Value;
string g1 = m.Groups[1].Value;
string g2 = m.Groups[2].Value;
if (g2 != null && g2.Length > 0)
{
res.Add(g2);
}
else
{
// get the quoted string, but without the quotes (in
g1);
res.Add(g1);
}
}
return (string[])res.ToArray(typeof(string));
}

--
William Stacey [C# MVP]

| Hi all,
|
| I've got a csv file for numeric data, some of which are greater than
| 10^3. Some bright fellow trying to br helpful put US-standard commas in
| these numbers, and to maintain the correct cell-index put quotation
| marks around the comma'd result.
|
| Example csv line:
|
| 43.56,345.2,"1,285,100",45.6
|
| I would like to replace the quoted-comma'd numbers with their
| unquoted-uncomma'd versions. I'm reading the csv line-by-line, putting
| a line into a string called csvLine. I'd like to call the static
| Regex.Replace method, in a way such as the following:
|
| Regex.Replace(csvLine, quotedCommadNumberPattern,
| noQuotesNoCommasPrevArg)
|
| I can come up with the regular expression for the second argument. But
| I'm not sure about the third. In particular, how can I make a regular
| expression refer to "the digits in what was found in the previous
| argument"?
|
| Or am I going about this all wrong?
|
| Thanks for any ideas,
|
| cdj
|
 
sherifffruitfly said:
Example csv line:

43.56,345.2,"1,285,100",45.6

I would like to replace the quoted-comma'd numbers with their
unquoted-uncomma'd versions. I'm reading the csv line-by-line, putting

Another way to achieve the goal is using of MatchEvaluator.

ASP.Net 2.0
string pattern = "\"([0-9.,]+)\"";
string testStr = "43.56,345.2,\"1,285,100\",45.6";
MatchEvaluator eval = delegate (Match match)
{
return match.Groups[1].Value.Replace(",","");
};
testStr = new Regex(pattern).Replace(testStr, eval);


ASP.Net 1.1
void foo()
{
string pattern = "\"([0-9.,]+)\"";
string testStr = "43.56,345.2,\"1,285,100\",45.6";
testStr = new Regex(pattern).Replace(testStr, new
MatchEvaluator(eval));
}

static string eval(Match match)
{
return match.Groups[1].Value.Replace(",", "");
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top