문자열의 모든 숫자를 일치시키려면 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