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 4 25178
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
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
|
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(",", "");
}
marss wrote:
Another way to achieve the goal is using of MatchEvaluator.
Thanks for the ideas folks - most helpful!
cdj This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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:...
|
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...
|
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):
...
|
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...
|
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"...
|
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'...
|
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)?
|
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...
|
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...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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,...
|
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,...
|
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,...
|
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...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
| | |