How to Find Percentage Symbol with Regex
This article shows how to find percentage symbols in C# strings using the regular expressions (Regex). You will see how to find single as well as multiple percentage symbols within C# strings. You will also see how to find percentage symbols along with numbers and how to extract numbers only that occur with percentage symbols.
Finding a Single Percentage Sign
The Match() method from the Regex class of the System.Text.RegularExpression module can be used to search for a single percentage symbol within a string. The regular expression used in this case is “%”.
The input string and the regex expression are passed as the first and second arguments, respectively, to the Match() method.
If a match is found the Match() method returns an object with Success attribute set to true. You can then print the index of the percentage symbol using the Index attribute. Here is an example:
using System; using System.Text.RegularExpressions; namespace RegexCodes { class Program { static void Main(string[] args) { string input = "john got 55% marks in exam, while sara obtained 40%."; string regex = @"%"; var result = Regex.Match(input, regex); if (result.Success == true) { Console.WriteLine("Percentage symbol found at index: " + result.Index.ToString()); } Console.ReadLine(); } } }
Output:
Finding Multiple Percentage Signs
You can also find multiple percentage signs with the help of Matches() method from the regex class. The regex expression used in this case is: “%”.
The Matches() method returns a collection of Match objects where each object corresponds to one percentage symbol found in the input string.
To check if any match is found, you can print the value of the Count attribute of the collection returned by the Matches() method.
Finally, you print the index of each percentage symbol using the Index attribute of the Match objects from the list of Match objects returned by the Matches() function.
Here is an example script that prints indexes of all the percentage symbols in the input string.
using System; using System.Text.RegularExpressions; namespace RegexCodes { class Program { static void Main(string[] args) { string input = "john got 55% marks in exam, while sara obtained 40%."; string regex = @"%"; var result = Regex.Matches(input, regex); if (result.Count > 0) { Console.WriteLine("Total count of percentage symbols found: " + result.Count); int i = 1; foreach(Match m in result) { Console.WriteLine("Percentage symbol "+ i + " found at index: " + m.Index.ToString()); i++; } } Console.ReadLine(); } } }
In the output below, you can see the indexes of the two percentage symbols from our input string.
Output:
Finding Percentages with Numbers
An important task in string parsing is the extraction of numbers that occur with percentage symbols. Regex in C# allows you to find such numbers. You can use the Matches() method once again. However, the regex expression used will be “([0-9]*%)”. This regex expression checks for all the numbers that occur together with a percentage symbol.
Here is an example where a regex expression is used to extract digits along with percentage symbols from within a C# string.
using System; using System.Text.RegularExpressions; namespace RegexCodes { class Program { static void Main(string[] args) { string input = "john got 55% marks in exam, while sara obtained 40%."; string regex = @"([0-9]*%)"; var result = Regex.Matches(input, regex); if (result.Count > 0) { Console.WriteLine("Total count of percentage amounts found: " + result.Count); int i = 1; foreach(Match m in result) { Console.WriteLine("Percentage amount "+ i + " - " + m.Value); i++; } } Console.ReadLine(); } } }
Now in the output below, you can see numbers along with the percentage symbols.
Output:
Finding Numbers only from Number with Percentages
Sometimes you need to extract the digit part only from the numbers that occur together with percentage symbols. You can do so with the help of regular expressions.
The regex expression used in this case inside with the Matches() method is “([0-9]+)*(%)”. This regex expression looks for two groups of co-occurring matches i.e numbers from 0 to 9 and the percentage symbols.
Next, from the list of Match objects returned by the Matches() method, you can search the Groups collection to extract the digit part only. In this case, the digit part occurs at the first index in the Groups collection.
The script below returns the digit part only from all the numbers that occur together with a percentage symbol in the input string.
using System; using System.Text.RegularExpressions; namespace RegexCodes { class Program { static void Main(string[] args) { string input = "john got 55% marks in exam, while sara obtained 40%."; string regex = @"([0-9]+)*(%)"; var result = Regex.Matches(input, regex); if (result.Count > 0) { Console.WriteLine("Total count of percentage amounts found: " + result.Count); int i = 1; foreach(Match m in result) { Console.WriteLine("Percentage amount "+ i + " - " + m.Groups[1].Value); i++; } } 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#
- How to Find Brackets with Regex
- How to Find Hash Sign with Regex
- How to Find Percentage Symbol with Regex