Regex, replace nth field in a CSV record

B

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
 
B

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
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 said:
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
 
B

bob

Hi All,
Decided to iterate through the Matches group collection.
Don't waste any time on this question.
Thanks
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
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 said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top