Normalizing strings + make array

  • Thread starter Thread starter Gustaf Liljegren
  • Start date Start date
G

Gustaf Liljegren

I'd like a simple way of extracting everything that's not whitespace on each
line in a file. For example, if the program encounters this line

1 2\t\t3 \t\t4

I want a string array like this

{"1", "2", "3", "4"}

I tried working with String.Split() but found that it doesn't automatically
remove the empty elements in the resulting array. I haven't found any
normalize method in the API either, to replace the sequences of two or more
whitespace characters to a single space. Hope there's something I've
overlooked.

Thanks,

Gustaf
 
Gustaf,

You might want to take a look at regular expressions for this. There
are escape sequences which can eliminate whitespace (I believe \w) and then
will split apart the string appropriately.

However, someone else in the group might be better suited to providing
the actual pattern. You will have to use the Regex class in the
System.Text.RegularExpressions namespace.

Hope this helps.
 
Gustaf,

Check out System.Text.RegularExpressions.Regex. You probably want to match
against "\S*".

-Marc
 
Nicholas Paldino said:
You might want to take a look at regular expressions for this. There
are escape sequences which can eliminate whitespace (I believe \w) and then
will split apart the string appropriately.

\w is "word characters". \s is whitespace. (I say, after trying \w and
being surprised when it didn't work :)
However, someone else in the group might be better suited to providing
the actual pattern. You will have to use the Regex class in the
System.Text.RegularExpressions namespace.

I believe the pattern wanted here is @"\s+" with the string being
trimmed to start with.

Here's a sample program:

using System;
using System.Text.RegularExpressions;

class Test
{
static void Main()
{
string t = " 1 2\t\t3 \t\t4 ";

string[] x = Regex.Split (t.Trim(), @"\s+");
foreach (string s in x)
{
Console.WriteLine ("'{0}'", s);
}
}
}

That produces the output:

'1'
'2'
'3'
'4'
 
Jon,

A more straightforward approach would be to use a matching regex, @"\S+",
non-whitespace words:

string thing = " 1 2\t\t3 \t\t4 john\ndoe h.e-r_e's s;o:m'e other words ";

MatchCollection words = Regex.Matches(thing, "\\S+");

foreach (Match m in words)

Console.WriteLine(m.Value);

-Marc

Jon Skeet said:
Nicholas Paldino said:
You might want to take a look at regular expressions for this. There
are escape sequences which can eliminate whitespace (I believe \w) and then
will split apart the string appropriately.

\w is "word characters". \s is whitespace. (I say, after trying \w and
being surprised when it didn't work :)
However, someone else in the group might be better suited to providing
the actual pattern. You will have to use the Regex class in the
System.Text.RegularExpressions namespace.

I believe the pattern wanted here is @"\s+" with the string being
trimmed to start with.

Here's a sample program:

using System;
using System.Text.RegularExpressions;

class Test
{
static void Main()
{
string t = " 1 2\t\t3 \t\t4 ";

string[] x = Regex.Split (t.Trim(), @"\s+");
foreach (string s in x)
{
Console.WriteLine ("'{0}'", s);
}
}
}

That produces the output:

'1'
'2'
'3'
'4'
 
Jon Skeet said:
Here's a sample program:

Thanks! Very helpful indeed.
string[] x = Regex.Split (t.Trim(), @"\s+");

Yes, this works fine.

But I just found another obstacle. The syntax in the input files allows
fields to be quoted if they contains spaces. So this

1 "2 3"\t\t4 \t\t5

should be splitted into

{"1", "2 3", "4", "5"}

This means whitespace should be removed only if it appears outside double
quotes (U+0034). And I'm a regex newbie. Maybe this is a question for
comp.lang.awk, but if someone knows how to contruct this regex, please post.
:-)

Thanks,

Gustaf
 
Gustaf Liljegren said:
But I just found another obstacle. The syntax in the input files allows
fields to be quoted if they contains spaces. So this

1 "2 3"\t\t4 \t\t5

should be splitted into

{"1", "2 3", "4", "5"}

This means whitespace should be removed only if it appears outside double
quotes (U+0034). And I'm a regex newbie. Maybe this is a question for
comp.lang.awk, but if someone knows how to contruct this regex, please post.
:-)

Ah - at that stage, I would personally ditch regular expressions and
write the parsing bit yourself. It shouldn't be too hard and the code
shouldn't be too slow afterwards.
 
Jon,

I told you that someone else would be better suited in crafting the
regular expression! =)


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Jon Skeet said:
Nicholas Paldino said:
You might want to take a look at regular expressions for this. There
are escape sequences which can eliminate whitespace (I believe \w) and then
will split apart the string appropriately.

\w is "word characters". \s is whitespace. (I say, after trying \w and
being surprised when it didn't work :)
However, someone else in the group might be better suited to providing
the actual pattern. You will have to use the Regex class in the
System.Text.RegularExpressions namespace.

I believe the pattern wanted here is @"\s+" with the string being
trimmed to start with.

Here's a sample program:

using System;
using System.Text.RegularExpressions;

class Test
{
static void Main()
{
string t = " 1 2\t\t3 \t\t4 ";

string[] x = Regex.Split (t.Trim(), @"\s+");
foreach (string s in x)
{
Console.WriteLine ("'{0}'", s);
}
}
}

That produces the output:

'1'
'2'
'3'
'4'
 
Ah - at that stage, I would personally ditch regular expressions and
write the parsing bit yourself. It shouldn't be too hard and the code
shouldn't be too slow afterwards.

I wrote some code that needed to do this as well - my solution was still to
split with the Regex, but then I do some checking to look for double quotes
at the start and end of the strings, and when I find a pair I just join
them back together. Of course this means you will lose the knowledge of
exactly WHAT whitespace ("was it a tab or a space?") was separating them,
but in my case it didn't matter... just a thought.

-mbray
 
Back
Top