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.
Instead of using
Regex.Split, it might be easier to match the tokens you need:Split may add empty matches, as you’ve witnessed, that will have to be filtered out.