473,756 Members | 3,686 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(c svLine, quotedCommadNum berPattern,
noQuotesNoComma sPrevArg)

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 25219
Can you be explicitly use String.Replace than Regex.Replace?

chanmm

"sherifffruitfl y" <sh************ *@gmail.comwrot e in message
news:11******** **************@ 48g2000cwx.goog legroups.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(c svLine, quotedCommadNum berPattern,
noQuotesNoComma sPrevArg)

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.WriteLi ne(s1);
public static string[] SplitQuoted(str ing 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 ArgumentNullExc eption("text", "text is null.");
if (delimiters == null || delimiters.Leng th < 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.Re gularExpression s.Match m in
System.Text.Reg ularExpressions .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(ty peof(string));
}

--
William Stacey [C# MVP]

"sherifffruitfl y" <sh************ *@gmail.comwrot e in message
news:11******** **************@ 48g2000cwx.goog legroups.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(c svLine, quotedCommadNum berPattern,
| noQuotesNoComma sPrevArg)
|
| 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
1783
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: 1,3,6,12,13,14,15 Any help ??
7
8317
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 curiosity, did a quick scan of an ASCII file of the 542 URLs in my personal bookmark file and discovered exactly 3 that contained commas (two with a single comma, one with three commas) -- so I guess they're pretty rarely used, even if legal. Seems...
36
2659
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): options { directory "/var/named"; listen-on { 192.168.11.22; 127.0.0.1; }; forwarders { 168.126.63.1; };
0
80710
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 usually have this problem but Dates can too. Debug.Print Me.ControlName refers to a control on a form. Whereas, Debug.Print "Me.ControlName" simply prints the literal text Me.ControlName Where to Use Each Quote Type (' or ") In some places...
14
7091
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" action="<%=request.servervariables("Script_name") %>" <% for i - 0 to 4%> Header: <input type="text" name="header" id="header<%=i%>"
3
2670
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' 199 'something else' any ideas on how to do this? thanks.
15
1273
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
8887
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 and commas, and can use quotes to encapsulate multiple words. An example: tag1, tag2 tag3, "tag4 tag4, tag4" tag5, "tag6 tag6" So, as we can see here anything is allowed, but the problem is that splitting on commas obviously destroys tag4...
14
1919
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 constants set with quotes around them, while "class constants" (those little things I learned yesterday from the article) don't have to have quotes around the constants when you define them? It seems really weird to me why PHP would do it this...
0
9456
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9275
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10034
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9872
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9843
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8713
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7248
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
3805
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2666
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.