嗨,我正在尝试从下面的标签中获取结果,我需要实现的是在标签中获得第一个匹配,然后是第五个匹配,然后是第九个匹配,所以是第一个,然后是每第五个匹配。所以我的结果是,注意我意识到这不是解析 HTML 的最佳方式,但我真的只需要它
我正在使用的正则表达式是
<td class="stat">(.*?)<\/td>
我正在使用的代码是
private static ObservableCollection<Top> top = new ObservableCollection<Top>();
public void twit_topusers_DownloadStringCompleted(Object sender, DownloadStringCompletedEventArgs e)
{
string str;
// Size the control to fill the form with a margin
str = (string)e.Result;
Regex r = new Regex("<td class=\"stat\">(.*?)</td>");
// Find a single match in the string.
Match m = r.Match(str);
while (m.Success)
{
testMatch = "";
//
testMatch += System.Text.RegularExpressions.Regex.Unescape(m.Groups[0].ToString()).Trim();
top.Add(new Top(testMatch));
m = m.NextMatch();
}
listBox.ItemsSource = top;
}
}
标签是
<td class="stat">14307149</td>//FIRST
<td class="stat">679761</td>
<td class="stat">3508</td>
<td class="stat">62 months ago</td>
<td class="stat">1430700</td>//FIFTH
<td class="stat">679761</td>
<td class="stat">3508</td>
<td class="stat">72 months ago</td>
<td class="stat">1430600</td>//NINTH
<td class="stat">679761</td>
<td class="stat">3508</td>
<td class="stat">82 months ago</td>
但我得到的结果是
匹配 1 14307149
比赛 2 679761
第 3 场比赛 3508
第 4 场比赛 62 个月前
匹配 5 1430700
比赛 6 679761
比赛 7 3508
第 8 场比赛 72 个月前
比赛 9 14307149
比赛 10 679761
比赛 11 3508
第 12 场比赛 62 个月前
我需要的结果是
匹配 1 14307149
匹配 2 1430700
匹配 3 1430600
你能帮我解决这个问题吗?