To match all digits in a string, use C# Regex.
Firstly, set a string with digits −
string str = "These are my marks: 90 out of 100!";
Use the following regular expression to get digits in a string −
@"\d+"
The following is the code −
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+");
Console.ReadKey();
}
}
}Output
Getting digits from a string... The Expression: \d+ 90 100