Link Search Menu Expand Document

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:

Reg Ex Find Percentage Symbol

 

 

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:

Reg Ex How to Find Percentage Symbol

 

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:

Regular Expression Find Percentage Symbol  

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:

Regular Expression How to Find Percentage Symbol  

Other useful articles:


Back to top

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