473,231 Members | 1,861 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,231 software developers and data experts.

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 25158
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Rimuen | last post by:
Have a string contains numbers from database. But there is similar numbers want to remove Example: 1,3,6,6,6,12,13,14,15,15,15,15 Want to remove the similar numbers so it would be like:...
7
by: AES | last post by:
Encountered a URL containing a comma the other day -- the first time I've ever noticed that, so far as I can recall. It worked fine, however, and I gather commas are legal in URLs. Out of...
36
by: Roman Mashak | last post by:
Hello, All! I implemented simple program to eliminate entry from the file having the following structure (actually it's config file of 'named' DNS package for those who care and know): ...
0
NeoPa
by: NeoPa | last post by:
Background Whenever code is used there must be a way to differentiate the actual code (which should be interpreted directly) with literal strings which should be interpreted as data. Numbers don't...
14
by: Adrienne Boswell | last post by:
Although this is a client side issue, I am also posting to asp.general in case there is someway to do this only server side (which I would prefer). Here's my form: <form method="post"...
3
by: joe | last post by:
hello I have a string that has values in quotes 'hello' 'here' '199' 'something else' I need to remove the quotes only when they are around numbers. To make the string lookg like 'hello' 'here'...
15
by: Szabolcs | last post by:
Newbie question: Why is 1 == True and 2 == True (even though 1 != 2), but 'x' != True (even though if 'x': works)?
9
by: conspireagainst | last post by:
I'm having quite a time with this particular problem: I have users that enter tag words as form input, let's say for a photo or a topic of discussion. They are allowed to delimit tags with spaces...
14
by: adam.timberlake | last post by:
This is a really basic question for all you people out there who know PHP. This is not a problem but just something I'm confused about. I was reading the article below and wondered why are normal...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.