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