Reputation: 1390
I've been searching for a good guide on this, but I can't quite figure out the regex syntax.
I have the following string that I need parsed:
[ 2013.11.22 22:50:30 ] System > Firstname Surname was kicked by Moderator
The variables I need to pull out should look a bit like this:
[ <yyyy>.<MM>.<dd> <hh>:<mm>:<ss> ] System > <username> was kicked by <moderatorname>
So, basically the timestamp and who was kicked by who (alpha-numeric names). Here's what's confusing me a little. Both the username and the name of the moderator could potentially be in 2 or even 3 parts divided by spaces... and potentially I guess the username could be "was kicked" which surely could screw with the parsing.
I haven't done alot of regex before, so I'm not that good at the syntax. Looking at a few guides I've come this far:
string text = "[ 2013.11.22 22:50:30 ] System > Firstname Surname was kicked by Moderator"
var input = text.ToLower();
Match m = Regex.Match(input, @"(?i:\[\s)(?<year>\d{4})\.(?<month>\d{1,2})\.(?<day>\d{1,2})\s(?<hour>\d{1,2})\:(?<minute>\d{1,2})\:(?<second>\d{1,2})\s\]");
This works for parsing the timestamp, but the text part following is giving me some trouble. I'm not really sure how to approach the issue.
Any help is appreciated, thank you
Upvotes: 1
Views: 118
Reputation: 6531
It may be your intention to only use regex, if so, fair enough. Otherwise may I suggest this could be simpler for the date part.
string date = "2013.11.22 22:50:30";
DateTime dateTime = DateTime.ParseExact(date , "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture);
or use DateTime.Parse() if there's less certainty about the format.
I'll have a look at one big regex, but my approach would be to just pickup the usernames with regex with something like this:
System > (((?!System|\swas).)+)\swas (whoops, I'm picking up addition things)
and
(?<=kicked by).*
Upvotes: 1
Reputation: 11126
use this :
\[\s*(?<yyyy>\d+)\.(?<MM>\d+)\.(?<dd>\d+)\s+(?<hh>\d+)\:(?<mm>\d+)\:(?<ss>\d+)\s+\] System > (?<username>.+) was kicked by (?<moderatorname>\w+)
demo here :
Upvotes: 2