求个简单的正则,求网友赐代码

yzmsb 2010-07-18 06:50:29
<td id='8888888888888888' align="center" colspan="8"
bgcolor="#cccccc">
.......................................
<div id="dgReshop_di"><table cellspacing="1" cellpadding="1" width="100%" align="center" border="0"><tr style='background-color:#F7F7F7;height:13px;' bgcolor='#F7F7F7'><td align="center" width="30%">测试项目</td>...............<td bgcolor='white' align="center">固定名称-输出内容二</td><td align="center">0</td><td align="center">15888888888</td></tr></table></div>
</td>
</tr>
</td>
</tr><tr>
<td>


目的输出内容为(三项,一是16位数字,二是文字中间有"-"的,三是手机号):8888888888888888 固定名称-输出内容二 15888888888

...全文
65 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
兔子-顾问 2010-07-18
  • 打赏
  • 举报
回复
自问自答的很好。或是
static void Main(string[] args)
{
string test = @"<td id='8888888888888888' align=""center"" colspan=""8""
bgcolor=""#cccccc"">
.......................................
<div id=""dgReshop_di""><table cellspacing=""1"" cellpadding=""1"" width=""100%"" align=""center"" border=""0""><tr style='background-color:#F7F7F7;height:13px;' bgcolor='#F7F7F7'><td align=""center"" width=""30%"">测试项目</td>...............<td bgcolor='white' align=""center"">固定名称-输出内容二</td><td align=""center"">0</td><td align=""center"">15888888888</td></tr></table></div>
</td>
</tr>
</td>
</tr><tr>
<td>";
MatchCollection mc = Regex.Matches(test, @"(?ins)<td id='(?<id>[^']+).+?<td bgcolor='white' align=""center"">(?<item1>[^<]+)([^>]+>){4}(?<mobile>\d+)");
foreach(Match m in mc)
{
Console.WriteLine(m.Groups["id"].Value);
Console.WriteLine(m.Groups["item1"].Value);
Console.WriteLine(m.Groups["mobile"].Value);
}
Console.ReadKey();
}
yzmsb 2010-07-18
  • 打赏
  • 举报
回复
m = m.NextMatch();
yzmsb 2010-07-18
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 wuyazhe 的回复:]
C# code
static void Main(string[] args)
{
string test = @"<td id='8888888888888888' align=""center"" colspan=""8""
bgcolor=""#cccccc"">
……
[/Quote]

谢谢.再补充问题. test中有多个符合条件内容该如何写? 即Console.WriteLine(m.Groups["id"].Value);
有两个以上不同结果应该怎么分别输出呢?
兔子-顾问 2010-07-18
  • 打赏
  • 举报
回复
static void Main(string[] args)
{
string test = @"<td id='8888888888888888' align=""center"" colspan=""8""
bgcolor=""#cccccc"">
.......................................
<div id=""dgReshop_di""><table cellspacing=""1"" cellpadding=""1"" width=""100%"" align=""center"" border=""0""><tr style='background-color:#F7F7F7;height:13px;' bgcolor='#F7F7F7'><td align=""center"" width=""30%"">测试项目</td>...............<td bgcolor='white' align=""center"">固定名称-输出内容二</td><td align=""center"">0</td><td align=""center"">15888888888</td></tr></table></div>
</td>
</tr>
</td>
</tr><tr>
<td>";
Match m = Regex.Match(test, @"(?ins)<td id='(?<id>[^']+).+?<td bgcolor='white' align=""center"">(?<item1>[^<]+)([^>]+>){4}(?<mobile>\d+)");
if(!m.Success) return;
Console.WriteLine(m.Groups["id"].Value);
Console.WriteLine(m.Groups["item1"].Value);
Console.WriteLine(m.Groups["mobile"].Value);
Console.ReadKey();
}

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧