Scott
Scott

Reputation: 33

Splitting a string with regular expressions in .NET

I am in need of a regular expression that I can use to examine a string and return specific items when I do a RegEx.Split() in .NET. I've been trying to do this on my own, but I can never seem to get what I need, and the results never make any sense. Obviously I do not have a good handle on writing regular expressions.

So here is the string...

"%date - %-5level - [%thread] - %logger - %message - %exception%newline"

I essentially want to be returned an array that looks like the following:

"date"
"-5level"
"thread"
"logger"
"message"
"exception"
"newline"

The following code is close, but not quite.

Regex exp = new Regex(@"\W+");
string[] s = exp.Split(@"%date - %-5level - [%thread] - %logger - %message - %exception%newline");

I get the following:

""
"date"
"5level"
"thread"
"logger"
"message"
"exception"
"newline"

For some reason, I have an empty string as the first index, and the 3rd index is missing the "-". I assume because it is not a part of a "word".

The "-" aside for the moment, I then want to split "5level" into an array:

"5"
"level"

I experimented with this:

Regex exp2 = new Regex(@"(\d+)([a-zA-Z]+)");
string[] s2 = exp2.Split("5level");

But, it returns 2 indexes with empty strings in addition to the split items I want like so:

""
"5"
"level"
""

I'm stumped on how to format the expression to give me what I need. Any help would be appreciated.

Upvotes: 3

Views: 2446

Answers (2)

Haroun Hajem
Haroun Hajem

Reputation: 5628

A better way of doing this is to use Named Capturing Groups from RegEx engine and to filter out any empty matches in the Linq query.

MatchCollection matches = Regex.Matches(s, @"%(?<SomeName>[\w\-]+)");
string[] words = matches.Cast<Match>().Where(m => m.Length > 0 ).Select(m => m.Groups["SomeName"].Value).ToArray();

Upvotes: 0

Kobi
Kobi

Reputation: 137997

Instead of using Regex.Split, it might be easier to match the tokens you need:

MatchCollection matches = Regex.Matches(s, @"%([\w\-]+)");
string[] words = matches.Cast<Match>().Select(m => m.Groups[1].Value).ToArray();

Split may add empty matches, as you've witnessed, that will have to be filtered out.

Upvotes: 4

Related Questions