tags:

views:

158

answers:

5

I have a String like this:

"WAIT_UNTIL;SYSTEM_TIME >= Di 15 Sep 2009    23:00:21 and IgnKeyPos ==3"

or something like this

"IF;Thermal >=20 and SYSTEM_TIME >= Tue 16 Sep 2009    23:00:21 "

I need to extract only the Time and Date part so I can use it later like this:

TimeThen = DateTime.Parse("Di 15 Sep 2009    23:00:21");

How should I start ?

+1  A: 

Consider using Regular Expressions

Here's some info on using them in C#:

Here's a sample usage

Regex dateTimeRegex = new Regex("\w<=(?<ParsedDateTime>YOUR REGEX GOES HERE)");

if(match.Success && match.Groups["ParsedDateTime"].Success)
{
   string parsedDateTime = match.Groups["ParsedDateTime"].Value;

   // process your parsed value here
}
TJB
That's one of the more useless answers I've read. Not even a mention of how to use regular expressions in C#? Particularly since, if the user hasn't already considered regular expressions, this would be completely unfamiliar territory to them.
Matt
I assumed its unfamiliar territory, that's why I included a link to a good place to quickly learn about regex. I wasn't trying to be condescending or anything like that, i just wanted to point him in the right direction. While it may not be the best answer I think its a good first step. Including an example in C# would have been better I agree, but I was going to leave it up to him to see how to apply it in his case instead of just give him 'teh codez'. While your comment has a borderline offensive tone, I'll gladly take the constructive criticizm and probably do deserve a -1.
TJB
@Matt, I updated the question with more info and an example
TJB
A: 
(?<trash>.*?)<?<arrow>\s\>\=\s)(?<dow>\w{2,3})\s*(?<day>\d{1,2})\s*(?<month>\w{3})\s*(?<time>\d{1,2}\:\d{1,2}\:\d{1,2}).*

something like this regex for the first one?

Henri
@Henri: fixed it for you. You just had to put it in a code block... different ways to do it: (1) highlight it and click the icon, or (2) press CTRL+K, or (3) indent with 4 spaces.
Ahmad Mageed
+3  A: 

This would match, although with more feedback on the expected format it could be enhanced. For now it accepts at least 1 space between the date/time parts.

string input = "IF;Thermal >=20 and SYSTEM_TIME >= Tue 15 Sep 2009    23:00:21 ";
string pattern = @"[A-Z]+\s+\d+\s+[A-Z]+\s+\d{4}\s+(?:\d+:){2}\d{2}";
Match match = Regex.Match(input, pattern, RegexOptions.IgnoreCase);

if (match.Success)
{
    string result = match.Value;
    DateTime parsedDateTime;
    if (DateTime.TryParse(result, out parsedDateTime))
    {
     // successful parse, date is now in parsedDateTime
     Console.WriteLine(parsedDateTime);
    }
    else
    {
     // parse failed, throw exception
    }
}
else
{
    // match not found, do something, throw exception
}
Ahmad Mageed
A: 
private static DateTime ExtractDateTime(string format)
{
    int length = format.Length;
    for (int startIndex = 0; startIndex < length; startIndex++)
    {
        for(int subLength = length - startIndex; subLength > 0; subLength--)
        {
            string substring = format.Substring(startIndex, subLength);
            DateTime result;
            if (DateTime.TryParse(substring, out result))
            {
                return result;
            }
        }
    }
    return DateTime.MinValue;
}
Jacob Seleznev
A: 

Thank you all for your quick response.

I'm impressed.

Just pasted Ahmads code into my project and it works.

Great forum here

tomfox66