Link Search Menu Expand Document

How to Find Time Using Regular Expression in C#

Extracting time from a text document can be an important task when you want to keep track of the duration of various tasks mentioned in a text document. For instance, you may want to track the time taken by a document to reach from its source to its destination. In such cases, you can either pour through a text document manually or you can automatically extract time from a text document. In the case of a huge number of documents, manual time extraction can be cumbersome and prone to error.

In this article, you will see how you can find time from a text document using C# regular expression. You will first see how to read a text document and then find the time without a specific format and later with a specific format.

Table of Contents

  1. Finding Time without a Specific Format
  2. Finding Time with a Specific Format

Finding Time without a Specific Format

To find time without a specific format, the simplest way is to identify the text after which the time is most likely to be mentioned. For instance, if you look at the invoice below, you can see that the time occurs after the text “Time : “.  Regex can then be used to find anything that occurs after the text “Time :”.

Find Time

The regex expression that you can use to find the time from the above text document is “Time : ([\w:]+)”. This regex expression returns all the digits, letters, and the colon (:) characters, that follow the text “Time : “. Here you added a colon “:” to the regex pattern because minutes, hours, and seconds are separated by colons in the above text invoice.

using System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;

namespace RegexCodes
{
class Program
{
static void Main(string[] args)
{
string textFile = File.ReadAllText(@"E:\Datasets\invoice.txt", Encoding.UTF8);

Console.WriteLine("====================");
var myRegex = new Regex(@"Time : ([\w:]+)", RegexOptions.IgnoreCase);

string result = myRegex.Match(textFile).ToString();

Console.WriteLine(result);


}
}
}

In the output below, you can see the time returned by the regex pattern.

Output:

How to Find Time

Finding Time with a Specific Format

In the previous section, you saw how you can find time without any specific format. You can also use regex expressions to find time in a specific format. For instance, in the following text invoice, the time is in the format hh:mm:ss (hours: minutes: seconds). To find time in such a format, you need to write a regex expression that matches xx:xx:xx.

Reg Ex Find Time

The regex pattern that you can use to match the pattern xx:xx:xx is “\d{2}:\d{2}:\d{2}”. Look at the following script for reference. In the script below, you first use the File.ReadAllText() method which reads the text invoice and returns the text in the document in the form of a C# string. Next, the Match() method from regex is called to find the pattern xx:xx:xx inside the document text.

using System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;

namespace RegexCodes
{
class Program
{
static void Main(string[] args)
{
string textFile = File.ReadAllText(@"E:\Datasets\invoice.txt", Encoding.UTF8);

Console.WriteLine("====================");
var myRegex = new Regex(@"\d{2}:\d{2}:\d{2}", RegexOptions.IgnoreCase);

string result = myRegex.Match(textFile).ToString();

Console.WriteLine(result);
}
}
}


From the output below, you can see that the time in the format hh:mm:ss have been returned. You can try other time formats as well to see if you can find the desired time from a document.

Output:

Find Time Reg Ex

Other useful articles:


Back to top

© , Regexsonline.com — All Rights Reserved - Terms of Use - Privacy Policy