C# 程式來匹配字串中的所有數字
為了匹配字串中所有數字,使用 C#的正則表示式。
首先,設定一個包含數字的字串 −
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
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP