C# Program to match all the digits in a string

To match all digits in a string, we can use regular expressions (Regex) in C#. Regular expressions provide a powerful way to search for patterns in text, including numeric values.

The System.Text.RegularExpressions namespace contains the Regex class that allows us to define patterns and find matches within strings.

Syntax

Following is the syntax for matching digits using Regex −

MatchCollection matches = Regex.Matches(inputString, @"\d+");

The regular expression pattern \d+ breaks down as follows −

  • \d − matches any single digit (0-9)
  • + − matches one or more consecutive occurrences

Regex Pattern \d+ Breakdown \d Any digit 0-9 + One or more consecutive +

Using Regex.Matches() Method

The Regex.Matches() method returns a MatchCollection containing all matches found in the input string −

Example

using System;
using System.Text.RegularExpressions;

namespace Demo {
   class Program {
      private static void showMatch(string text, string expr) {
         Console.WriteLine("The Expression: " + expr);
         MatchCollection mc = Regex.Matches(text, expr);
         foreach (Match m in mc) {
            Console.WriteLine(m);
         }
      }
      
      static void Main(string[] args) {
         string str = "These are my marks: 90 out of 100!";
         Console.WriteLine("Getting digits from a string...");
         showMatch(str, @"\d+");
      }
   }
}

The output of the above code is −

Getting digits from a string...
The Expression: \d+
90
100

Using LINQ with Regex

You can also use LINQ to work with the matches more efficiently −

Example

using System;
using System.Linq;
using System.Text.RegularExpressions;

class Program {
   static void Main() {
      string text = "Price: $25, Quantity: 3, Total: $75";
      
      var digits = Regex.Matches(text, @"\d+")
                       .Cast<Match>()
                       .Select(m => int.Parse(m.Value))
                       .ToArray();
      
      Console.WriteLine("Found digits: " + string.Join(", ", digits));
      Console.WriteLine("Sum of all digits: " + digits.Sum());
   }
}

The output of the above code is −

Found digits: 25, 3, 75
Sum of all digits: 103

Matching Single Digits vs Multi-Digit Numbers

Pattern Description Example Match
\d Matches single digits only In "123", matches 1, 2, 3 separately
\d+ Matches one or more consecutive digits In "123", matches 123 as one number
\d* Matches zero or more consecutive digits Matches empty strings too

Example

using System;
using System.Text.RegularExpressions;

class Program {
   static void Main() {
      string text = "Room 101, Floor 2, Building A5";
      
      Console.WriteLine("Using \d (single digits):");
      foreach (Match m in Regex.Matches(text, @"\d")) {
         Console.WriteLine("Match: " + m.Value);
      }
      
      Console.WriteLine("\nUsing \d+ (multi-digit numbers):");
      foreach (Match m in Regex.Matches(text, @"\d+")) {
         Console.WriteLine("Match: " + m.Value);
      }
   }
}

The output of the above code is −

Using \d (single digits):
Match: 1
Match: 0
Match: 1
Match: 2
Match: 5

Using \d+ (multi-digit numbers):
Match: 101
Match: 2
Match: 5

Conclusion

Regular expressions in C# provide an efficient way to match digits in strings using the \d+ pattern. The Regex.Matches() method returns all numeric sequences found in the text, which can be processed further using LINQ or simple iteration.

Updated on: 2026-03-17T07:04:35+05:30

447 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements