473,405 Members | 2,354 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,405 software developers and data experts.

Regex, replace nth field in a CSV record

bob
Hi
I need to replace the nth field in a CSV record with a new value,
Having trouble figuring out a regex that will capture the nth field.
I have a regex that captures every field in the record
but can't see an efficient way of making use of the matches collection
to build the required output string.
i.e. Don't want to iterate through the m[0].Groups collection
appending to a string builder when I figure I could use a replace
function if I had the right regex to count out n fields.

e.g.
MyRecord,"","","",""
replaced with
MyRecord,"","","NewValue",""

Any ideas?
Thanks
Bob
Aug 20 '07 #1
3 2491
Why don't you just parse the CSV into an array or a datatable and change it
there?

-- Peter
Recursion: see Recursion
site: http://www.eggheadcafe.com
unBlog: http://petesbloggerama.blogspot.com
BlogMetaFinder: http://www.blogmetafinder.com

"bob" wrote:
Hi
I need to replace the nth field in a CSV record with a new value,
Having trouble figuring out a regex that will capture the nth field.
I have a regex that captures every field in the record
but can't see an efficient way of making use of the matches collection
to build the required output string.
i.e. Don't want to iterate through the m[0].Groups collection
appending to a string builder when I figure I could use a replace
function if I had the right regex to count out n fields.

e.g.
MyRecord,"","","",""
replaced with
MyRecord,"","","NewValue",""

Any ideas?
Thanks
Bob
Aug 20 '07 #2
bob
Hi Peter,
I am reading these records out of a file, checking with a database
that certain fields match then writing the records (slightly altered)
out to another file.
I think the array suggestion would be almost as expensive as iterating
through the Matches group collection.
Am I to take it that there isn't a way to get the nth field with a
regex?
regards
Bob
On Mon, 20 Aug 2007 04:32:02 -0700, Peter Bromberg [C# MVP]
<pb*******@yahoo.yohohhoandabottleofrum.comwrote :
>Why don't you just parse the CSV into an array or a datatable and change it
there?

-- Peter
Recursion: see Recursion
site: http://www.eggheadcafe.com
unBlog: http://petesbloggerama.blogspot.com
BlogMetaFinder: http://www.blogmetafinder.com

"bob" wrote:
>Hi
I need to replace the nth field in a CSV record with a new value,
Having trouble figuring out a regex that will capture the nth field.
I have a regex that captures every field in the record
but can't see an efficient way of making use of the matches collection
to build the required output string.
i.e. Don't want to iterate through the m[0].Groups collection
appending to a string builder when I figure I could use a replace
function if I had the right regex to count out n fields.

e.g.
MyRecord,"","","",""
replaced with
MyRecord,"","","NewValue",""

Any ideas?
Thanks
Bob
Aug 20 '07 #3
bob

Hi All,
Decided to iterate through the Matches group collection.
Don't waste any time on this question.
Thanks
Bob
On Tue, 21 Aug 2007 08:50:41 +1200, bob
<st**************@cutthis.adriley.co.nzwrote:
>Hi Peter,
I am reading these records out of a file, checking with a database
that certain fields match then writing the records (slightly altered)
out to another file.
I think the array suggestion would be almost as expensive as iterating
through the Matches group collection.
Am I to take it that there isn't a way to get the nth field with a
regex?
regards
Bob
On Mon, 20 Aug 2007 04:32:02 -0700, Peter Bromberg [C# MVP]
<pb*******@yahoo.yohohhoandabottleofrum.comwrot e:
>>Why don't you just parse the CSV into an array or a datatable and change it
there?

-- Peter
Recursion: see Recursion
site: http://www.eggheadcafe.com
unBlog: http://petesbloggerama.blogspot.com
BlogMetaFinder: http://www.blogmetafinder.com

"bob" wrote:
>>Hi
I need to replace the nth field in a CSV record with a new value,
Having trouble figuring out a regex that will capture the nth field.
I have a regex that captures every field in the record
but can't see an efficient way of making use of the matches collection
to build the required output string.
i.e. Don't want to iterate through the m[0].Groups collection
appending to a string builder when I figure I could use a replace
function if I had the right regex to count out n fields.

e.g.
MyRecord,"","","",""
replaced with
MyRecord,"","","NewValue",""

Any ideas?
Thanks
Bob
Aug 20 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Jon Maz | last post by:
Hi All, Am getting frustrated trying to port the following (pretty simple) function to CSharp. The problem is that I'm lousy at Regular Expressions.... //from...
2
by: Daniel | last post by:
I use an Access database to basically take data exports, import them, manipulate the data, and then turn them into exportable reports. I do this using numerous macros, and queries to get the data...
6
by: tshad | last post by:
Is there a way to use Regex inside of a tag, such as asp:label? I tried something like this but can't make it work: <asp:label id="Phone" text=Regex.Replace('<%# Container.DataItem("Phone")...
4
by: Brian Henry | last post by:
I have phone numbers like this in a data table 123-435-1234 1231231234 432.234.2321 they all have different formatting, what I want to do is get them all formatted like this (123) 123-1234
24
by: cassetti | last post by:
Here's the issue: I have roughly 20 MS excel spreadsheets, each row contains a record. These records were hand entered by people in call centers. The problem is, there can and are duplicate...
11
by: Steve | last post by:
Hi All, I'm having a tough time converting the following regex.compile patterns into the new re.compile format. There is also a differences in the regsub.sub() vs. re.sub() Could anyone lend...
15
by: morleyc | last post by:
Hi, i would like to remove a number of characters from my string (\t \r \n which are throughout the string), i know regex can do this but i have no idea how. Any pointers much appreciated. Chris
2
by: nuffnough | last post by:
I am doing a string.replace in a simple table generation app I wrote, and I can't figure out how to match whitespace with /s, so I thought I would see if osmeone where would be kind enough to...
0
by: Karch | last post by:
I have these two methods that are chewing up a ton of CPU time in my application. Does anyone have any suggestions on how to optimize them or rewrite them without Regex? The most time-consuming...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
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...
0
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,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...

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.