Click here to Skip to main content
15,889,830 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
I've got text in a file as such:

MSIL
VMMAnalogs_Acquire.c                    1.26  2001_105/02  2001_105/02
    Analogs_Acquire                                  100%         100%
VMMAnalogs_Initialise.c                 1.25  2001_053/02  2001_053/02
    Analogs_InitialiseInput                          100%         100%


I want to write this data to a csv file, thus need to replace certain white space sections with a comma, as such:

VMMAnalogs_Acquire.c,1.26,2001_105/02,2001_105/02
    Analogs_Acquire,100%
VMMAnalogs_Initialise.c,1.25,2001_053/02,2001_053/02
    Analogs_InitialiseInput,100%


Here is code that replaces all white spaces with to get started:

MIDL
string input = listTxtFileContents[line_idx];
string pattern = "\\s+";
string replacement = ",";
Regex rgx = new Regex(pattern);
string result = rgx.Replace(input, replacement);

listTxtFileContents[line_idx] = result;


Can anyone help me please?

The problem that I'm facing is that the code replaces all the spaces with a single comma. I don't want it to replace the indentation of the 2nd and the 4th line's, words. (Analogs_Acquire and Analogs_InitialiseInput)

P.S. please vote up/down!
Posted
Updated 17-Nov-10 23:48pm
v2
Comments
Manfred Rudolf Bihy 18-Nov-10 5:21am    
I'd be glad to help you if only I knew what problems you have with your code.
You could at the very least specify what issues you're facing.
R. Erasmus 18-Nov-10 5:48am    
I've updated the code to contain the problem that I'm facing...

Try this;

MSIL
using System;
using System.Text;
using System.Text.RegularExpressions;
namespace SomeNameSpace
{
    class Program
    {
        private static string input = "VMMAnalogs_Acquire.c                    1.26  2001_105/02  2001_105/02\n" +
                               "Analogs_Acquire                                  100%         100%\n" +
                               "VMMAnalogs_Initialise.c                 1.25  2001_053/02  2001_053/02\n" +
                               "Analogs_InitialiseInput                          100%         100%\n";
        static void Main(string[] args)
        {
            Regex regExp = new Regex("\\s+");
            StringBuilder result = new StringBuilder();
            foreach (string line in input.Split('\n'))
            {
                result.AppendLine(regExp.Replace(line, ","));
            }
            Console.WriteLine(result);
        }
    }
}



Hope this helps,
Fredrik
 
Share this answer
 
Comments
R. Erasmus 18-Nov-10 6:33am    
Not quite was I was looking for... I realise that my original post might of been a little unclear.
I've posted the answer down below, thx.
I've found the solution to my problem and posted it down below, thanks!

C#
/* convert list buffer to csv (comma delimited) format */
for (line_idx = 0; line_idx < listTxtFileContents.Count; line_idx++)
{
  string input = listTxtFileContents[line_idx];
  string pattern = "\\s+";
  string replacement = ",";
  Regex rgx = new Regex(pattern);
  string result = rgx.Replace(input, replacement);
  listTxtFileContents[line_idx] = result;
  if (listTxtFileContents[line_idx].StartsWith(",") == true)
  {
    listTxtFileContents[line_idx] = listTxtFileContents[line_idx].Remove(0, 1);
    listTxtFileContents[line_idx] = "   " + listTxtFileContents[line_idx];
  }

}


P.S. VOTE - UP/DOWN ;)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900