【发布时间】:2015-09-28 14:07:07
【问题描述】:
我已经尝试了两天来解决问题,我有一个 MatchCollection。在模式中是一个组,我想要一个包含该组解决方案的列表(有两个或更多解决方案)。
string input = "<tr><td>Mi, 09.09.15</td><td>1</td><td>PK</td><td>E</td><td>123</td><td></td></tr><tr><td>Mi, 09.09.15</td><td>2</td><td>ER</td><td>ER</td><td>234</td><td></td></tr>";
string Patter2 = "^<tr>$?<td>$?[D-M][i-r],[' '][0-3][1-9].[0-1][1-9].[0-9][0-9]$?</td>$?<td>$?([1-9][0-2]?)$?</td>$?";
Regex r2 = new Regex(Patter2);
MatchCollection mc2 = r2.Matches(input);
foreach (Match match in mc2)
{
GroupCollection groups = match.Groups;
string s = groups[1].Value;
Datum2.Text = s;
}
但只有最后一个匹配项 (2) 出现在文本框“Datum2”中。 我知道我必须使用例如一个列表框,但 Groups[1].Value 是一个字符串...
感谢您的帮助和时间。 迪特
【问题讨论】:
-
您将 Datum2 文本字段替换为 s。它没有向 Datum2.Text 附加任何内容。如果您想查看所有匹配项,可以说 Datum2.Text = s + Datum2.Text
-
但是 Datum2 是一个空文本框。
-
只有第一个匹配项。
-
您是否考虑过使用正则表达式以外的其他工具来解析 HTML 字符串中的数据?您听说过 HtmlAgilityPack 吗?
-
匹配集合周围有循环。如果要在文本框中打印所有匹配项,请转换 match collection to string array 或使用 Datum2.Text = s + Datum2.Text