0

I am trying to parse out a DateTime from a string using a regular expression. My issue is that I have a list of strings which need to be sorted by their date in descending order. I want to find a regex that will get the date from the strings. They all look similar but I am not sure how to do it. All help is much appreciated.

This is what the string looks like:

sdfsad[10/16/2014 at 9:52 AM by AJOHNSON]sdfsadf

And I have a list that all have the same type of format.

This is the regex I have so far:

[0-9].[/][0-9].[0-9].*[at]

1
  • Your edit between versions 1 and 2 of the question is a little bit strong, I'd recommend completing the question next time, in order to make the progression more visible Commented Oct 16, 2014 at 19:55

2 Answers 2

2

If all of your lines take this form:

sdfsad[10/16/2014 at 9:52 AM by AJOHNSON]sdfsadf

Then I would suggest using a regular expression to grab everything from the [ to the space before by, and then pass that string to DateTime.ParseExact or DateTime.TryParseExact.

The regex to extract that bit of text should be straightforward, and using the DateTime parser to get the date and time will be easier than trying to do it with a regular expression.

Sign up to request clarification or add additional context in comments.

2 Comments

Can you help me with the regex? This is what i have but since there can be multiple dates in one string it does not stop right after the first bracketed date. "[0-9].*[/][0-9].*[0-9]... "
@user3788671: You'll have to show examples of strings you want to parse the date from. Your original question and the sample string say that there is one date that you want to use for the sort. If that's not correct, then you need to clarify. Edit your question and show more sample strings, including one that has multiple dates.
1

Use a regular expression to extract the components that interest you (date and time), create a datetime from these components and sort each line depending on the date time. you can use a temporary data class in order to help you create the structure you want

public class DataWithTimestamp
{
    public string line {get;set;}
    public DateTime stamp {get;set;}

    public DataWithTimestamp(string data)
    {
        stamp = regex.extract(pattern); // not the correct syntax, set it here
        line = data;
    } 
}

and in your code change your lines to the class, sort on the

var allLines = File.ReadAllLines("file.txt");
var sortedLines = allLines
                      .Select(s => new DataWithTimestamp(s))
                      .OrderBy(data => data.stamp)
                      .Select(data => data.line);

EDIT: this regex should work:

\[(?<date>[\d\/]+) at (?<time>[\d:]+).*\]

find the catpured groups date and time then ParseExact them into the correct DateTime

4 Comments

okay, I was trying to avoid that but it might be my only option
I don't really see another way, and it feels quite readable to me :)
Well there is also a lot of html element in the actual string but i think the brackets help a little bit with the parsing.
Any chance you can help me with the regex?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.