且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用正则表达式索引来匹配未知字符串?

更新时间:2022-11-15 11:34:10

hii

尝试Expresso工具

http://www.ultrapico.com/Expresso.htm [
hii

try Expresso tool

http://www.ultrapico.com/Expresso.htm[^]


因此,您拥有:
So, you have:
number out of number



数字是一个数字序列,至少包含一个数字:
正则表达式数字:\d
至少一个:+

现在您要提取这两个数字:(\d+)

全部放在一起(C#):



A number is a sequence of digits, with at least one digit:
Regex digit: \d
At least one: +

Now you my want to extract these two numbers: (\d+)

Putting all together (C#):

string pattern = @"(\d+) out of (\d+)";



获取全文(C#)中的所有匹配项:



Getting all matches in the whole text (C#):

string fullText = ...;
string pattern = @"(\d+) out of (\d+)";
int all = 0;
int sick = 0;
int count = 0;
foreach(var match in Regex.Matches(fullText, pattern).Cast<Match>())
{
    sick += int.Parse(match.Group[1].Value);
    all += int.Parse(match.Group[2].Value);
    count++;
}
Console.WriteLine("From {0} classes, {1}% are sick",
                   count, all > 0 ? sick / all : 0); 



干杯
安迪



Cheers
Andi