这是信息:
<tr><td height=27 align=center>现货铂金</td><td height=18 align=center>289.26</td><td height=18 align=center class=fontok>-1.04</td><td height=18 align=center>-0.36%</td><td height=18 align=center>290.61</td><td height=18 align=center>288.86</td></tr><tr><td height=27 align=center>现货钯金</td><td height=18 align=center>117.83</td><td height=18 align=center class=fontok>-0.55</td><td height=18 align=center>-0.46%</td><td height=18 align=center>118.48</td><td height=18 align=center>117.57</td></tr><tr><td height=27 align=center>现货白银</td><td height=18 align=center>5482</td><td height=18 align=center class=fontok>-21</td><td height=18 align=center>-0.38%</td><td height=18 align=center>5524</td><td height=18 align=center>5470</td></tr>
我想获取<tr><td height=27 align=center>现货铂金</td><td height=18 align=center>289.26</td><td height=18 align=center>-0.36%</td><tr>这样的数据,怎么做啊?我自己写的正则表达式,只能获取这一行,还显示两次。哪位大牛帮忙解决下啊,不胜感激
<tr><td height=27 align=center>现货铂金</td><td height=18 align=center>289.26</td><td height=18 align=center class=fontok>-1.04</td><td height=18 align=center>-0.36%</td><td height=18 align=center>290.61</td><td height=18 align=center>288.86</td></tr><tr><td height=27 align=center>现货钯金</td><td height=18 align=center>117.83</td><td height=18 align=center class=fontok>-0.55</td><td height=18 align=center>-0.46%</td><td height=18 align=center>118.48</td><td height=18 align=center>117.57</td></tr><tr><td height=27 align=center>现货白银</td><td height=18 align=center>5482</td><td height=18 align=center class=fontok>-21</td><td height=18 align=center>-0.38%</td><td height=18 align=center>5524</td><td height=18 align=center>5470</td></tr>
我想获取<tr><td height=27 align=center>现货铂金</td><td height=18 align=center>289.26</td><td height=18 align=center>-0.36%</td><tr>这样的数据,怎么做啊?我自己写的正则表达式,只能获取这一行,还显示两次。哪位大牛帮忙解决下啊,不胜感激
string str = @"<tr><td height=27 align=center>现货铂金</td><td height=18 align=center>289.26</td><td height=18 align=center class=fontok>-1.04</td><td height=18 align=center>-0.36%</td><td height=18 align=center>290.61</td><td height=18 align=center>288.86</td></tr><tr><td height=27 align=center>现货钯金</td><td height=18 align=center>117.83</td><td height=18 align=center class=fontok>-0.55</td><td height=18 align=center>-0.46%</td><td height=18 align=center>118.48</td><td height=18 align=center>117.57</td></tr><tr><td height=27 align=center>现货白银</td><td height=18 align=center>5482</td><td height=18 align=center class=fontok>-21</td><td height=18 align=center>-0.38%</td><td height=18 align=center>5524</td><td height=18 align=center>5470</td></tr>";
Regex reg = new Regex(@"(?is)<tr>(?:\s*<td[^>]*?>(.*?)</td>)*</tr>");
foreach (Match m in reg.Matches(str))
{
foreach (Capture c in m.Groups[1].Captures)
Console.WriteLine(c.Value);
Console.WriteLine(new string('*', 20));
}
/*
现货铂金
289.26
-1.04
-0.36%
290.61
288.86
********************
现货钯金
117.83
-0.55
-0.46%
118.48
117.57
********************
现货白银
5482
-21
-0.38%
5524
5470
********************
*/
foreach (Match m in reg.Matches(input,@"(?is)<tr>(<td[^>]*?>(.*?)</td>){1,}</tr>"))
{
foreach (Capture c in m.Groups[1].Captures)
Console.WriteLine(c.Value);
}
foreach (Match m in Regex.Matches(input,@"(?is)<tr>(<td[^>]*?>(.*?)</td>){1,}</tr>"))
{
foreach (Capture c in m.Groups[1].Captures)
Console.WriteLine(c.Value);
}
<tr><td height=27 align=center>现货铂金</td><td height=18 align=center>289.26</td><td height=18 align=center>-0.36%</td><tr>
try { Regex regexMobile = new Regex(@"<tr><td height=27 align=center>(.*?)</td><td height=18 align=center>(.*?)</td><td height=18 align=center class=fontok>(.*?)</td><td height=18 align=center>(.*?)</td><td height=18 align=center>290.61</td><td height=18 align=center>(.*?)</td></tr>",RegexOptions.Singleline); Match matchMobile = regexMobile.Match(strMobile); while (matchMobile.Success) { htmlMobile += @"<tr>"; htmlMobile += @"<td align=""center"">" + matchMobile.Groups[1].Value + "</td>"; htmlMobile += @"<td align=""center"">" + matchMobile.Groups[2].Value + "</td>"; htmlMobile += @"<td align=""center"">" + matchMobile.Groups[4].Value + "</td>"; htmlMobile += @"</tr>"; matchMobile = matchMobile.NextMatch(); } strMobile = null; matchMobile = null; } catch (ArgumentException ex) { } Response.Write(htmlMobile);
try
{
Regex regexMobile = new Regex(@"<tr><td height=27 align=center>(.*?)</td><td height=18 align=center>(.*?)</td><td height=18 align=center class=fontok>(.*?)</td><td height=18 align=center>(.*?)</td><td height=18 align=center>290.61</td><td height=18 align=center>(.*?)</td></tr>",RegexOptions.Singleline);
Match matchMobile = regexMobile.Match(strMobile);
while (matchMobile.Success)
{
htmlMobile += @"<tr>";
htmlMobile += @"<td align=""center"">" + matchMobile.Groups[1].Value + "</td>";
htmlMobile += @"<td align=""center"">" + matchMobile.Groups[2].Value + "</td>";
htmlMobile += @"<td align=""center"">" + matchMobile.Groups[4].Value + "</td>";
htmlMobile += @"</tr>";
matchMobile = matchMobile.NextMatch();
}
strMobile = null;
matchMobile = null;
}
catch (ArgumentException ex){ }
Response.Write(htmlMobile);
string pattern = @"^(?i)(<tr[^>]*?>(<td[^>]*?>[^<]*?</td>){2})((?!</tr>).)*?(<td[^>]*?>[^<]*?%[^<]*?</td>).*?(</tr>)";
tempStr = Regex.Replace(Regex.Match(tempStr,pattern).Value, pattern, @"$1$4$5");
/*
<tr><td height=27 align=center>现货铂金</td><td height=18 align=center>289.26</td><td height=18 align=center>-0.36%</td></tr> */
foreach(Match matchMobile in regexMobile.Matches(strMobile))
循环体中去掉
matchMobile = matchMobile.NextMatch();
不只是显示这一条数据,剩下两个<tr></tr>都显示
string tempStr = File.ReadAllText(@"C:\Documents and Settings\Administrator\桌面\Test.txt", Encoding.GetEncoding("GB2312"));//读取txt
string pattern = @"(?i)(<tr[^>]*?>(<td[^>]*?>[^<]*?</td>){2})((?!</tr>).)*?(<td[^>]*?>[^<]*?%[^<]*?</td>).*?(</tr>)";
string[] tempStr_list = Regex.Matches(tempStr, pattern).Cast<Match>().Select(a=>Regex.Replace(a.Value,pattern,"$1$4$5")).ToArray();
/*
[0] "<tr><td height=27 align=center>现货铂金</td><td height=18 align=center>289.26</td><td height=18 align=center>-0.36%</td></tr>" string
[1] "<tr><td height=27 align=center>现货钯金</td><td height=18 align=center>117.83</td><td height=18 align=center>-0.46%</td></tr>" string
[2] "<tr><td height=27 align=center>现货白银</td><td height=18 align=center>5482</td><td height=18 align=center>-0.38%</td></tr>" string */