使用正则表达式索引来匹配未知字符串? [英] Use regex index to match unknown strings?
问题描述
您好,我已经成功地从HTML输出中解析了该字符串,但是如果该字符串不存在,则会出现问题
例如,我有一个字符串,指出班上生病的孩子的人数.
"41中有3个",但是该数字总是在变化,因此我需要一种方法来标识特定中的"以及两个整数3和41,它们实际上只是字符串的一部分.
如果我不知道确切值,可以使用通配符表示数字吗?
我希望有人知道我要说的话,并且知道答案
Hello again I have successfully parsed the string from my HTML output but am having problems if the string does not exist
For example I have a string that states the number of kids in a class that are sick.
"3 out of 41" but that number always changes so I need a way to identify the "out of" and also the two integers 3 and 41 that are just part of the string really.
Can I somehow use a wild card to represent the numbers if I do not know there exact value?
I hope someone knows what I am trying to say and knows the answer
thank you in advance!
推荐答案
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
这篇关于使用正则表达式索引来匹配未知字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!