正则表达式匹配问题
采集源码如下:-------------------------------------------------------
<tr bgcolor="#FFFAF3">
<td height="30px" align="center">131017082</td>
<td align="center">2013-10-17 22:12</td>
<td align="center" style="padding-left:100px;">
<div class="ball_yellow">4</div>
<div class="ball_yellow">5</div>
<div class="ball_yellow">6</div>
</td>
<td align="center">15</td>
<td align="center" >
<a href="http://www.hao123.com" target="_blank"><img src="images/zoushi1.gif" width="17" height="17" border="0" /></a></td>
</tr>
---------------------------------------------------------------------------
以下是采集函数代码(需要采集的内容是131017082 和 4 5 6),不知道是不是正则表达式哪里错了,就是得不到我想要的结果:
public string[] get_shu(string qs = "")
{
string[] strArray = new string[2];
string pattern = "<tr.*?>[\\s\\S]*?<td.*?>(\\d{9})<\\/td>[\\s\\S]*?<td.*?>[\\s\\S]*?<div\\sclass=\"ball_yellow\">(\\d)<\\/div>[\\s\\S]*?<div\\sclass=\"ball_yellow\">(\\d)<\\/div>[\\s\\S]*?<div\\sclass=\"ball_yellow\">(\\d)<\\/div>[\\s\\S]*?<\\/td>[\\s\\S]*?<\\/tr>";
try
{
Regex regex = new Regex(pattern);
if (!qs.Equals(""))
{
foreach (Match match in regex.Matches(this.msg))
{
if (match.Groups[1].Value == qs)
{
strArray[0] = match.Groups[1].Value;
strArray[1] = string.Concat(new object[] { Convert.ToInt16(match.Groups[2].Value), ",", Convert.ToInt16(match.Groups[3].Value), ",", Convert.ToInt16(match.Groups[4].Value) });
return strArray;
}
}
return strArray;
}
Match match2 = regex.Match(this.msg);
if (match2.Success)
{
strArray[0] = match2.Groups[1].Value;
strArray[1] = string.Concat(new object[] { Convert.ToInt16(match2.Groups[2].Value), ",", Convert.ToInt16(match2.Groups[3].Value), ",", Convert.ToInt16(match2.Groups[4].Value) });
}
}
catch (Exception exception)
{
Console.WriteLine(exception.ToString());
}
return strArray;
}
请高手帮忙! 正则表达式 regex exception --------------------编程问答-------------------- 我试了是对的
get_shu()[0]=131017082
get_shu()[1]=4,5,6
你跟踪看看this.msg的值,看程序怎么走的
*****************************************************************************
签名档: http://feiyun0112.cnblogs.com/ --------------------编程问答--------------------
string sInput = File.ReadAllText(@"C:\Users\myx\Desktop\Test.txt",Encoding.GetEncoding("GB2312"));
var list = Regex.Matches(sInput, @"<tr[^>]*?>\s*?<td[^>]*?>([^<>]*?)</td>[\s\S]*?(\s*?<div[^>]*?class=(['""]?)ball_yellow\3[^>]*?>(?<ball>[^<>]*?)</div>\s*?)+[\s\S]*?</tr>").OfType<Match>().Select(a => new {
phone=a.Groups[1].Value,
ball=string.Join(" ",a.Groups["ball"].Captures.Cast<Capture>().Select(b=>b.Value))
});
/*
+ [0] { phone = "131017082", ball = "4 5 6" } <Anonymous Type>
*/
补充:.NET技术 , ASP.NET