How to Find Quantity Field Using Regular Expression in C#
If you have a large number of invoices, detecting the quantity of the items sold in each invoice may take time. If you want to find the sum of quantities of items sold in all the invoices, you will have to manually read quantity values from all the invoices and then sum them together. Reading quantity values from text documents is cumbersome and time-consuming.
This article shows how you can use C# regular expressions to find the value of the quantity field from a text document. Let’s begin without any ado.
Consider a scenario where you have the following text invoice. The invoice contains the prices of various fruits, the total tax amount, the quantity of all the items, and the invoice number.
Reading the value of the quantity field from the above script depends highly on the invoice text. In the above text document, the quantity is mentioned after the word "Quantity :". To read such an invoice, you can use the regex patterns that read the string “Quantity : ” followed by any digit. The regex pattern which can be used to do so is
Quantity : \s\d.
The regex patterns tell the regex to include the string “Quantity :” followed by any number of spaces and then numbers, in the final output. This regex pattern is then passed to the “Match()” function of the regex module as shown in the following script. At the beginning of the following script, you read the text document using the File.ReadAllText() method. The File.ReadAllText() method returns all the text in a document in the form of a string.
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(@"(Quantity :\s*\d*)", RegexOptions.IgnoreCase); string result = myRegex.Match(textFile).ToString(); Console.WriteLine(result); } } }
In the output below you can see the Quantity field along with the quantity value.
Output:
Let’s now take another example. The quantity field can be mentioned using some other words as well. For instance, in the following invoice, the word “Item Quantity” is mentioned to specify the item quantity.
In such cases, you need to update your regex expression accordingly. Previously, you used the regex expression Quantity :\s\d since the item quantity was mentioned after the word “Quantity : “. Now in the updated invoice, the quantity value is mentioned after the word “Item Quantity”. Hence, you need to update your regex expression as Item Quantity :\s\d. The Match() method is again called using the updated regex expression and the result is converted into the string format. Look at the following script.
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(@"(Item Quantity :\s*\d*)", RegexOptions.IgnoreCase); string result = myRegex.Match(textFile).ToString(); Console.WriteLine(result); } } }
From the output below, you can see that the “Item Quantity” field is retrieved along with the quantity value i.e. 04. Depending upon the text used for the quantity field in your text document or invoice, you can update your regex expression accordingly.
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