Link Search Menu Expand Document

How to Find Total Tax Using a Regular Expression in C#

This article explains how to find total tax from text documents using C# with the help of regular expressions. The article shows how to extract the tax amount that is present with or without currency symbols in a text document.

Finding Total Tax Containing Numbers Only

Consider a scenario where you have the following receipt. Here the tax contains only a number i.e. 15 and no currency symbol or decimal point.

Getting Started Image

To find tax from the above document, you can use the following regex expression.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
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(@"(Total Tax: \s*\d*)", RegexOptions.IgnoreCase);

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

            Console.WriteLine(result);

        }
    }
}

The above script reads a text file that contains tax information and then retrieves any string of numbers that follows the word “Total Tax”.  The Regex expression used to retrieve all a continuous list of numbers is “\s\d”. Here “\s” stands for any space characters after “Total Tax” and “\d” returns all the numbers after space.  Here is the output of the above script.

====================
Total Tax: 15

Finding Total Tax Containing Currency Symbols and Decimals

Tax information often contains currency symbols and decimals,  for instance in the following invoice:

Getting Started Image

To read tax information in such a format, you need to modify the regex expression as follows:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
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(@"(Total Tax:\s*\$?\s*\d*(\.\d{1,3}))", RegexOptions.IgnoreCase);

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

            Console.WriteLine(result);
        }
    }
}

The regular expression used in the above script is “(Total Tax:\s$?\s\d*(.\d{1,3}))”. Here $? specifies that the expression may or may not start with a “$” sign. The expression “.” searches decimal whereas “\d{1,3}” looks for up to three decimal places in the text.  The output shows the total tax including the dollar sign and decimal points.

====================
Total Tax: $15.25

In the script above, the word “Total Tax” is also displayed with the tax amount. If you wish to only retrieve the tax amount, you can use the following script.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
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(@"(Total Tax Amount:\s*\$?\s*\d*(\.\d{1,3}))", RegexOptions.IgnoreCase);

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

            string total_tax= result.Split(":")[1];
            Console.WriteLine(total_tax.Trim());

        }

    }
}

In the script above the string returned by the regular expression is split using a colon and then the second part of the string i.e. the one containing the amount is printed. In the output below, you can see the tax amount only.

====================
$15.25

Other useful articles:


Back to top

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