Link Search Menu Expand Document

How to Extract Amount with Currency Symbols using Regex in C#

In this article, you will see how to extract amounts with currency symbols from text strings using the regular expressions, regex in C#.

While parsing invoices and receipts, you might see different types of currency symbols. In some scenarios, it is important to retrieve currency symbols along with the amount, particularly if an invoice contains an amount in multiple currencies.

In this article, you will see three scenarios where you will extract amounts and currency symbols:

  1. Extracting currency symbols only
  2. Extracting amount with currency symbols
  3. Extracting amount only

Finding Currency Symbols Only

To see what are the different currency symbols that exist within a string, you can use the Matches() method and pass it your input string and the regex expression.

The regex expression used for matching currency symbols is “\p{Sc}”.

Let’s see an example. The input string in the script below contains three currency symbols: dollar ($), euro (€), and pound (£). Run the script below and see the output:

Note: You will need to import the “System.Text.RegularExpressions” module before running the script below.

    class Program
    {
        static void Main(string[] args)
        {

            string input = "This is $45 and €25, you will also have to pay £10";

            string regex = @"\p{Sc}";

            var matches = Regex.Matches(input, regex);

            if (matches.Count > 0)
            {
                Console.WriteLine("Match found:");

                foreach (Match m in matches)
                {
                    Console.WriteLine("- " +m.Value);
                }
            }

            Console.ReadLine();

        }
    }

 

Output

Regular Expressions Find Amount

From the above output, you can see that the symbols for dollar ($) and pound (£) are displayed but a question mark “?” is displayed in place of the symbol for euro (€). To display the symbol for the euro (€), you need to set the value of the “Console.OutputEncoding” attribute to “System.Text.Encoding.UTF8” format as shown in the script below:

            if (matches.Count > 0)
            {
                Console.OutputEncoding = System.Text.Encoding.UTF8;
                Console.WriteLine("Match found:");

                foreach (Match m in matches)
                {
                    Console.WriteLine("- " +m.Value);
                }
            }

 

Output

Regular Expression Find Amount

 

Now from the output above, you can see that the value for the euro (€) is displayed on the console.

Finding Amount with Currency Symbols

You can also find the amount along with the currency symbol using the Regex. Let’s see an example.

In the script below, the input string contains three amounts along with currency symbols: $45, €25, and, £10. You can see that the currency symbols are prefixed or come before the amount.

You can use the regex expression “(\p{Sc}) ([0-9])” to extract the amount along with the currency symbols. This regular expression has two parts: (\p{Sc}) and ([0-9]). The first part searches for all the currency symbols. The second part searches for all the digits that occur right after the currency symbols.

    class Program
    {
        static void Main(string[] args)
        {

            string input = "This is $45 and €25, you will also have to pay £10";

            string regex = @"(\p{Sc}) *([0-9]*)";

            var matches = Regex.Matches(input, regex);

            if (matches.Count > 0)
            {
                Console.OutputEncoding = System.Text.Encoding.UTF8;
                Console.WriteLine("Match found:");

                foreach (Match m in matches)
                {
                    Console.WriteLine("- " +m.Value);
                }
            }

            Console.ReadLine();

        }
    }

 

Output

Reg Ex Find Amount

In the output above, you can see the amount along with the currency symbols.

 

Finding Amount Only

Finally, you can also extract the amount only without the currency symbols using the Regex. The regex expression that you can use is the same that you saw in the previous section i.e. “(\p{Sc}) ([0-9.])”.

The Matches() function returns a collection of all the matches in the form of Match objects. The Groups collection for each Match object contains different components of the matched string.

In our script, the amount is stored at the third index of the Group collection of each match object. You can then use the Value attribute to print the Value at any index in the Group collection.

The following script prints only the amount, without a currency symbol, from your input string.

    class Program
    {
        static void Main(string[] args)
        {

            string input = "This is $45 and €25, you will also have to pay £10";

            string regex = @"(\p{Sc}) *([0-9.]*)";

            var matches = Regex.Matches(input, regex);

            if (matches.Count > 0)
            {
                Console.OutputEncoding = System.Text.Encoding.UTF8;
                Console.WriteLine("Match found:");

                foreach (Match m in matches)
                {
                    Console.WriteLine("- " +m.Groups[2].Value);
                }
            }

            Console.ReadLine();

        }
    }

 

Output

Reg Exp Find Amount  

Other useful articles:


Back to top

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