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
  • 0 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

We have some methods that call File.Copy, File.Delete, File.Exists, etc. How can we test
I have DocumentRoot /var/www/test in my .htaccess file. This is causing the apache server
I have a PHP file, Test.php, and it has two functions: <?php echo displayInfo();
If I have a Resource bundle property file: A.properties: thekey={0} This is a test
This is driving me crazy. I have this one php file on a test
Very simply put, I have the following code snippet: FILE* test = fopen(C:\\core.u, w);
Let's say I have a file called test.txt within the package com.test.io within my
For simplicity, i have the following file named test.jsp: <script language=javascript> alert(a$b.replace(/\$/g,k)); </script> I
For example, suppose I have a batch file called 'test.cmd' and it simply contains:
I have written a program in Java to read in a text file of

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.