Link Search Menu Expand Document

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.

RegEx Quantity Field

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:

Regular Expression Quantity Field

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.

Reg Ex Quantity Fields

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:

Quantity Field Reg Ex

Other useful articles:


Back to top

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