Link Search Menu Expand Document

How to Find a Number Using Regular Expressions in C#

A text string is the most versatile data type in C#. A string can store alphabets, numbers, spaces, special characters, in short virtually everything in the form of a sequence of characters. Often times you need to find a specific data type within a string. For instance, you might want to look for special characters, numbers, or boolean values inside a string. Regular expressions can be used for such tasks.

In this article, you will see how to find numbers within a text string using regular expressions in C#. Therefore, let’s begin without ado.

Finding all Numbers in a String

To find all the numbers in a string, you can use the Split() function from Regex. The first parameter to the Split() function is the string that contains numbers that you want to find. The second parameter is the regex expression used to find numbers in a string. The regex expression “\D+” returns numbers in a string in the form of an array.

Look at the following example. In the following script,  the input string contains 4 numbers. The Regex Split() function will return an array that contains the numbers in the input string. The numbers are then printed on the console via a for each loop.

using System;
using System.Text.RegularExpressions;

namespace RegexCodes
{
   class Program
   {
      static void Main(string[] args)
      {
         string input = "A year has 365 days, 52 weeks. There are 
7 days in a week. A week has 24 hours";

         string[] numbers = Regex.Split(input, @"\D+");
         foreach (string num in numbers)
            Console.WriteLine(num);
      }

   }
}

Here is the output of the above script:

365
52
7
24

Finding a Number Between Two Strings

Often times you need to find a number between two strings. Finding a number between two strings is actually similar to finding a string between two strings. Once a number is found, you can convert the number from string to integer.

To find a number between two strings, you can use the Match() function from Regex. The first parameter to the Match() function is the string that contains numbers that you want to find; the second parameter is the Regex pattern that finds a number between two string values.

The following script tries to find a number that exists between two strings “has” and “days”.

using System;
using System.Text.RegularExpressions;

namespace RegexCodes
{
   class Program
   {
      static void Main(string[] args)
      {
         string input = "A year has 365 days and 52 weeks.";

         string match = Regex.Match(input, @"has (.+?) days 
").Groups[1].Value;
         match = match.Trim();

         int num_match = int.Parse(match);
         Console.WriteLine(match);
      }

   }
}

The Match() function returns a string. In the above script, to convert string to numbers we first remove all the empty spaces from the string using the Trim() function. Next, int.Parse() function is used to convert the string to an integer. Finally, the integer is printed on the screen.

In the above script, we have a number 365 between the strings “has” and “days”. When you run the above script, you should see the following output:

365

 

Other useful articles:


Back to top

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