111,126
社区成员
发帖
与我相关
我的任务
分享
<tr>
<td><a href="./userInfo.asp?id=1234">name1</a></td>
<td>22岁</td>
<td>77</td>
<td>
<form id="form1" name="form1" action="./doForm.asp" method="post">
<input type="hidden" name="id" value="1234"/>
<a href="javascript:document.form1.submit();" >删除</a>
</form>
</td>
</tr>
<tr>
<td><a href="./userInfo.asp?id=12345">name2</a></td>
<td>24岁</td>
<td>56</td>
<td>
<form id="form1" name="form2" action="./doForm.asp" method="post">
<input type="hidden" name="id" value="12345"/>
<a href="javascript:document.form2.submit();" >删除</a>
</form>
</td>
</tr>
<tr>
<td><a href="./userInfo.asp?id=123">name3</a></td>
<td>21岁</td>
<td>89</td>
<td>
<form id="form1" name="form3" action="./doForm.asp" method="post">
<input type="hidden" name="id" value="1234"/>
<a href="javascript:document.form3.submit();" >删除</a>
</form>
</td>
</tr>
string strRegex = @"<tr[^</tr>]+.*?<td>[^</td>]+.*?id=(?<userID>\d+)"">(?<userName>\.+)</a>.*?</td>.*?<td>[^</td>]+(?<Age>\d+)岁</td>.*?<td>[^</td>]+(?<Points>\d+)</td>.*?</tr>";
MatchCollection mc = Regex.Matches(str, strRegex,RegexOptions.Multiline);
txtBoxUserInfo.Text = mc.Count.ToString();
foreach (Match m in mc)
{
txtBoxUserInfo.Text += m.Groups["userID"].Value + " - " + m.Groups["userName"].Value + " - " + m.Groups["Age"].Value + " - " + m.Groups["Points"].Value + "\r\n";
}
string strRegex = @"<tr[^<]*.*?<td>.*?id=(\d+)"">(.*?)</a></td>.*?<td>(\d+)岁</td>.*?<td>(\d+)</td>.*?</tr>";
[^<]* 这个是什么意思呢
后来改成<tr[^<>]* 都可以。
string strRegex = @"<tr[^<]*.*?<td>.*?id=(\d+)"">(.*?)</a></td>.*?<td>(\d+)岁</td>.*?<td>(\d+)</td>.*?</tr>";
[^<]* 这个是什么意思呢
后来改成<tr[^<>]* 都可以。
string strRegex = @"<tr[^<]*.*?<td>.*?id=(\d+)"">(.*?)</a></td>.*?<td>(\d+)岁</td>.*?<td>(\d+)</td>.*?</tr>";
MatchCollection mc = Regex.Matches(str, strRegex, RegexOptions.Singleline);
string strRegex = @"<tr[^</tr>]+.*?<td>[^</td>]+.*?id=(\d+)"">(.*?)</a></td>.*?<td>(\d+)岁</td>.*?<td>(\d+)</td>.*?<td>.*?</td>.*?</tr>";
MatchCollection mc = Regex.Matches(str, strRegex, RegexOptions.Singleline);
foreach (Match m in mc)
{
txtBoxUserInfo.Text += m.Groups[1].Value + " - " + m.Groups[2].Value + " - " + m.Groups[3].Value + " - " + m.Groups[4].Value + "\r\n";
}
我
再把 strRegex = "["+strRegex+"]?"; 后能得到很多了,但是莫名其妙的是, 每2条或者N条才能获得一个结果
也就是说,foreach (Match m in mc) 里的每个m.ToString() 不只一个<tr></tr> 可能包含好几行 但不管包含多少行,匹配到一个结果