Parsing text containing tabs

  • Thread starter Thread starter Johan Goris
  • Start date Start date
J

Johan Goris

What is the best way to parse text containing tabs by using Regex?

Is this already correct? What can be added?

private string[] SplitsOp(ref string teDoen)

{

int counter = 1;

int count1;

Regex expression = new Regex(@".*\t");

string[] s = new string[count1]; // I want to equal count1 with the count
of tabs.

do{

s[counter] = expressionMatch( .... // what's needed here?

}while (++counter <= count1)

return s;

}
 
string[] s = Regex.Split(teDoen, @"\t");

Why use regular expressions at all?

string[] s = s.Split ('\t');

Or if you're going to do it many times:

// Do this once
static readonly char[] TabOnly = {'\t'};

// and then in the method

string[] s = s.Split (TabOnly);

(This prevents .NET from having to create a new array with just a tab
in every time you execute it.)
 
string[] s = Regex.Split(teDoen, @"\t");

Why use regular expressions at all?

Jon,

Oversight on my part. I meant to type:

string[] s = Regex.Split(teDoen, @"\t+");

This would handle cases where more than one tab separates the
individual text elements. With this regex, no empty string array
elements would be created when multiple sequential tabs are
encountered. If Johan's data will never contain multiple sequential
tabs, then your solution is the way to go.


Chris.
 
Jon,

I forgot something else too...

Congratulations on the MVP Award!

Chris.
 
Back
Top