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
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 :”.
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:
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.
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:
Other useful articles:
- How to Use RegEx for Data Extraction
- How to Find Total Tax Using a Regular Expression in C#
- How to Find a Number Using Regular Expressions in C#
- How to Find Invoice Numbers Using Regular Expressions in C#
- Find SSN Using a Regular Expression in C#
- Find Total Amount Using a Regular Expression in C#
- How to Find Website Links using Regex
- Module 1: Regular Expressions for Beginners
- Module 1: Regex Usage and Tool Demo
- Module 2: Regex Engine Basics (Part 1)
- Module 2: Regex Engine Basics (Part 2)
- Module 2: Regex Syntax in Detail (Part 1)
- Module 2: Regex Syntax in Detail (Part 2)
- Module 2: Quantifiers in Reg Ex for Beginners
- Module 2: Short Codes in Reg Ex for Beginners
- Module 2: Anchors and Boundaries in Detail
- Module 2: Grouping and Subpattern in Detail
- Module 3: Realtime Use Case of Regular Expressions - Part 1
- Module 3: Realtime Use Case of Regular Expressions - Part 2
- Module 3: Realtime Use Case of Regular Expressions - Part 3
- Module 3: Realtime Use Case of Regular Expressions - Part 4
- How to Find Quantity Field Using Regular Expression in C#
- How to Find Phone Numbers without a Specific Format
- How to Find Date Using Regular Expression in C#
- How to Find Time Using Regular Expression in C#
- How to Find a Sentence Using Regular Expressions in C#
- Find a Word Using Regular Expression in C#
- Find a Due Date using Regular Expressions in C#
- How to Find the End of a String Using Regular Expression in C
- How to Find the Start of a String Using Regular Expression in C
- How to Find a Comma using Regular Expression in C Sharp
- How to Find a Dot using Regular Expression in C
- How to Find a Semicolon using Regular Expression in C Sharp
- How to Find a Double Space using Regular Expression in C