Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 351593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T11:39:29+00:00 2026-05-12T11:39:29+00:00

I have a test file that has many lines, each line looks something like:

  • 0

I have a test file that has many lines, each line looks something like:

4:19 PM     5:15 PM  this is some text blah blah

I need a regex that will pull the 2 times and assign them to a variable.

So basically i’m going to be looping through a text file, and extracting the time information from each line and adding up the difference between the two.

I need help with the regex.

  • 1 1 Answer
  • 2 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T11:39:30+00:00Added an answer on May 12, 2026 at 11:39 am

    This uses a slightly tweaked regex and adds the differences between dates as you mentioned:

    string[] input = { "4:19 PM         5:15 PM  this is some text blah blah",
                       "3:00 PM         5:00 PM  text"
                     };
    
    // build up pattern
    string datePattern = @"(\d+:\d+\s(?:AM|PM))";
    string pattern = String.Format("^{0}{1}{2}{3}$",
                                   datePattern, @"\s+", datePattern, @"\s+.*");
    
    TimeSpan total = new TimeSpan();
    foreach (string text in input)
    {
        var match = Regex.Match(text, pattern);
        if (match.Success)
        {
            // skip first group which has entire match
            DateTime dt1 = DateTime.Parse(match.Groups[1].Value);
            DateTime dt2 = DateTime.Parse(match.Groups[2].Value);
            TimeSpan diff = dt2 - dt1;
            total += diff;
        }
    }
    Console.WriteLine("Total difference: {0}", total);
    

    Result: Total difference: 02:56:00


    If you’re confident that your data is in the correct format you could make the regex very simple as follows:

        string text = "4:19 PM         5:15 PM  this is some text blah blah";
        string pattern = @"(?<time>\d+:\d+)\s(?<period>AM|PM)";
    
    foreach(Match m in Regex.Matches(text, pattern))
    {
        Console.WriteLine("Time: {0} - Period: {1}",
            m.Groups["time"].Value,
            m.Groups["period"].Value
            );
    }
    

    If you don’t trust the data then you’ll probably want something more robust.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an xml file that contains its element like <ab:test>Str</ab:test> When I am
I have 2 actions in a Flash file that I would like to test
I have this test code that just saves an XML file to a folder.
I have an ASCII binary file which looks something like: 00010110001001000110011001000111 01011000011100001010100001001000 11110001011010000010010101111010 00000000000000000000000000000000
I have a test file that contains tests taking quite a lot of time
I have a test file that contains 1,2,3 2,3,4 5,6,7 I want to insert
I have a js file test.js that contains a global variable global_var and a
I have a LDIF file that consists of a set of test users and
I have one project inside that I have SqlServerFunctions in test.cs file. code: [Microsoft.SqlServer.Server.SqlFunction]
so i have ruby script that simply writes to a test.txt file with hello

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.