表内容如下:
<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>
想要提取
第一行 的id 和 name
第二行 的岁数
第三行 的分数我写的代码如下:
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";
}
这样匹配不出来,达人们赐教,这样的正则该如何写,才能提取出所要的数据,谢谢!!!
<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>
想要提取
第一行 的id 和 name
第二行 的岁数
第三行 的分数我写的代码如下:
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";
}
这样匹配不出来,达人们赐教,这样的正则该如何写,才能提取出所要的数据,谢谢!!!
解决方案 »
- DevExpress.XtraGrid.GridControl中数据源的绑定问题
- c#菜鸟请教
- [超难]C#里如何注入系统进程(如SVCHOST.EXE)
- winform如何修改配置文件app.config
- sql2005如何另存为sql2000格式?
- DiertoryInfo在那个命名空间下
- VS 2010 工具箱 没有控件,重新安装仍然没有,怎么办啊?
- 如何在RichTexBox中显示一个无限分类的树型结构?(解决即刻解贴!!!!!)
- 设置控件的Dock属性为什么使得其绘制了多份?
- 如何将dataTable绑定到一个有DataGridViewComboBoxColumn的datagridview控件上??
- 问个简单的问题,gridview控件中,我现在有两个选择的linkbutton,我需要在选择事件中判断是那一个按钮触发了事件怎么做?
- C#中数据的存储
或者用XPath类的也可以啊,XPathNavigator
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> 可能包含好几行 但不管包含多少行,匹配到一个结果
string strRegex = @"<tr[^<]*.*?<td>.*?id=(\d+)"">(.*?)</a></td>.*?<td>(\d+)岁</td>.*?<td>(\d+)</td>.*?</tr>";
MatchCollection mc = Regex.Matches(str, strRegex, RegexOptions.Singleline);
.ReadToEnd().Replace("\r\n", "");
Regex reg =
new Regex("<tr>.*?href=\".*?id=(.*?)\">(.*?)<.*?<td>([0-9]*)岁</td>.*?<td>([0-9]*)</td>.*?</tr>"
, RegexOptions.Compiled | RegexOptions.Multiline);
Match mat = null;int index = 0;
while ((mat = reg.Match(text, index)).Success)
{
Console.WriteLine("ID:{0}; Name:{1}; Age:{2}; Mark:{3}",
mat.Groups[1].Value,
mat.Groups[2].Value,
mat.Groups[3].Value,
mat.Groups[4].Value);
index = mat.Index + mat.Length;
}
.ReadToEnd().Replace("\r\n", "");
Regex reg =
new Regex("<tr>.*?href=\".*?id=(.*?)\">(.*?)<.*?<td>([0-9]*)岁</td>.*?<td>([0-9]*)</td>.*?</tr>"
, RegexOptions.Compiled | RegexOptions.Multiline);
MatchCollection mtcol = reg.Matches(text);
foreach(Match mat in mtcol)
{
Console.WriteLine("ID:{0}; Name:{1}; Age:{2}; Mark:{3}",
mat.Groups[1].Value,
mat.Groups[2].Value,
mat.Groups[3].Value,
mat.Groups[4].Value);
}
[^<]* 这个是什么意思呢
后来改成<tr[^<>]* 都可以。
稍等,结贴散分
string strRegex = @"<tr[^<]*.*?<td>.*?id=(\d+)"">(.*?)</a></td>.*?<td>(\d+)岁</td>.*?<td>(\d+)</td>.*?</tr>";
[^<]* 这个是什么意思呢
后来改成<tr[^<>]* 都可以。