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
热门推荐
10 财运虎年祝福语简短语
11 单亲爸爸春节祝福语简短
12 狗狗上厕所训练
13 植物种植祝福语简短
14 翻译考试祝福语朋友简短
15 个人周年庆简短祝福语
16 狗狗怎么训练
17 分别祝福语简短搞笑英文
18 生日广州男生祝福语简短