एक स्ट्रिंग में सभी अंकों का मिलान करने के लिए, C# Regex का उपयोग करें।
सबसे पहले, अंकों के साथ एक स्ट्रिंग सेट करें -
string str = "These are my marks: 90 out of 100!";
एक स्ट्रिंग में अंक प्राप्त करने के लिए निम्नलिखित रेगुलर एक्सप्रेशन का प्रयोग करें -
@"\d+"
निम्नलिखित कोड है -
उदाहरण
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();
}
}
} आउटपुट
Getting digits from a string... The Expression: \d+ 90 100