469,328 Members | 1,301 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,328 developers. It's quick & easy.

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

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

Dec 22 '06 #1
4 24869
Can you be explicitly use String.Replace than Regex.Replace?

chanmm

"sherifffruitfly" <sh*************@gmail.comwrote in message
news:11**********************@48g2000cwx.googlegro ups.com...
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
Dec 25 '06 #2
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[i] = sa1[i].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]

"sherifffruitfly" <sh*************@gmail.comwrote in message
news:11**********************@48g2000cwx.googlegro ups.com...
| 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
|
Dec 25 '06 #3

sherifffruitfly wrote:
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(",", "");
}

Dec 25 '06 #4

marss wrote:
Another way to achieve the goal is using of MatchEvaluator.
Thanks for the ideas folks - most helpful!

cdj

Dec 26 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by AES | last post: by
36 posts views Thread by Roman Mashak | last post: by
14 posts views Thread by Adrienne Boswell | last post: by
3 posts views Thread by joe | last post: by
15 posts views Thread by Szabolcs | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by Purva khokhar | last post: by
reply views Thread by haryvincent176 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.