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:
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
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
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
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
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
- How to Split Text Using Regex
- How to Find HTML Tags Using Regex
- How to Validate Email Address via Regex in C#
- How to Extract Amount with Currency Symbols using Regex in C#