正则表达式 在线等 本帖最后由 ligengrong 于 2010-07-17 09:30:30 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 (语言\d+)(?:[^>]*>){2}([^<]+)取分组1,2 private static void TestRegex08() { string test = @"<table cellpadding=""0"" cellspacing=""1"" class=""tb_1""> <tr><th width=""92"">语言1</th><td width=""211"">保加利亚语<td><td width=""105"">熟练<td><td width=""360"">这个语言呢,我描述一下感想。</td></tr><tr><th width=""92"">语言2</th><td width=""211"">蒙古语<td><td width=""105"">熟练<td><td width=""360"">这个蒙古语呢,强大,我来描述下</td></tr> </table>(?is)<tr>[^<]*<th[^>]*>语言([^>]+>){2}(?<language_name>[^<\r\n]+)([^>]*>){2}(?<language_grade>[^<\r\n]+)([^>]*>){2}(?<language_description>[^<\r\n]*)"; MatchCollection mc = Regex.Matches(test, @"(语言\d+)(?:[^>]*>){2}([^<]+)"); foreach (Match m in mc) { Console.WriteLine(m.Groups[1].Value + " : " + m.Groups[2].Value); } } 大哥 可以不可以用key 就来取的!? 以为 我要所有中文信息都要取到!? 多谢你每次的帮忙!thanks 自己添加一下不就行了。都取出来了。不用key,你还容易些啊。 private static void TestRegex08() { string test = @"<table cellpadding=""0"" cellspacing=""1"" class=""tb_1""> <tr><th width=""92"">语言1</th><td width=""211"">保加利亚语<td><td width=""105"">熟练<td><td width=""360"">这个语言呢,我描述一下感想。</td></tr><tr><th width=""92"">语言2</th><td width=""211"">蒙古语<td><td width=""105"">熟练<td><td width=""360"">这个蒙古语呢,强大,我来描述下</td></tr> </table>(?is)<tr>[^<]*<th[^>]*>语言([^>]+>){2}(?<language_name>[^<\r\n]+)([^>]*>){2}(?<language_grade>[^<\r\n]+)([^>]*>){2}(?<language_description>[^<\r\n]*)"; MatchCollection mc = Regex.Matches(test, @"(语言\d+)(?:[^>]*>){2}([^<]+)"); Dictionary<string, string> dict = new Dictionary<string, string>(); foreach (Match m in mc) { dict.Add(m.Groups[1].Value, m.Groups[2].Value); } Console.WriteLine(dict["语言1"]); } 大哥 我是说 在正则表达式内的?<language_name>这些! 可以做成这样的效果么!? (?<zz>)这叫命名捕获组,如果组很多,一般会用这个,否则,就用下标就很清晰了。你这次的和之前的不同。你无法写一个很长很长的来用一次匹配成功。即便用一次匹配,找多个捕获,一样要写循环的。 你测试过我给你的没有?难道不是同时?m.Groups[1].Value 就是 语言1m.Groups[2].Value 就是 语言1对应的值不明白你是一定要给这个加个名字还是什么意思 哦, 我只是想 在html里太多这样的格式 同时我也需要每组去去出不同的内容,记录没个信息! 可以取到了!原来(?is)和(?nis)的作怪 大哥可以解释一下么!? (?isn)i:忽略大小写s:.匹配回车换行,表示单行模式n:取消未命名的分组。 vs2008 c++编译成静态库dll失败的原因 求模拟鼠标单击的WinAPI 复制文件用进度条表示 出现异常进度条不走---请帮忙看看程序(全部代码再附件里) datagridview checkbox GDI+中怎样让一个正方体旋转? 急急急!!!大家帮忙! WinForm中的DataGrid的问题?请各位帮帮忙!!!!!!!!!!!!!!!!!!!!! 客户端如何控制datagrid隐藏不同行? 关于用XSD模式验证XML文档的链接初步问题 关于mediaplayer多线程控制的问题 关于&运算符问题 求解 如何实现textbox下拉列表功能
{
string test = @"<table cellpadding=""0"" cellspacing=""1"" class=""tb_1"">
<tr><th width=""92"">语言1</th><td width=""211"">保加利亚语<td><td width=""105"">熟练<td><td width=""360"">这个语言呢,我描述一下感想。</td></tr><tr><th width=""92"">语言2</th><td width=""211"">蒙古语<td><td width=""105"">熟练<td><td width=""360"">这个蒙古语呢,强大,我来描述下</td></tr>
</table>(?is)<tr>[^<]*<th[^>]*>语言([^>]+>){2}(?<language_name>[^<\r\n]+)([^>]*>){2}(?<language_grade>[^<\r\n]+)([^>]*>){2}(?<language_description>[^<\r\n]*)";
MatchCollection mc = Regex.Matches(test, @"(语言\d+)(?:[^>]*>){2}([^<]+)");
foreach (Match m in mc)
{
Console.WriteLine(m.Groups[1].Value + " : " + m.Groups[2].Value);
}
}
大哥 可以不可以用key 就来取的!? 以为 我要所有中文信息都要取到!? 多谢你每次的帮忙!thanks
{
string test = @"<table cellpadding=""0"" cellspacing=""1"" class=""tb_1"">
<tr><th width=""92"">语言1</th><td width=""211"">保加利亚语<td><td width=""105"">熟练<td><td width=""360"">这个语言呢,我描述一下感想。</td></tr><tr><th width=""92"">语言2</th><td width=""211"">蒙古语<td><td width=""105"">熟练<td><td width=""360"">这个蒙古语呢,强大,我来描述下</td></tr>
</table>(?is)<tr>[^<]*<th[^>]*>语言([^>]+>){2}(?<language_name>[^<\r\n]+)([^>]*>){2}(?<language_grade>[^<\r\n]+)([^>]*>){2}(?<language_description>[^<\r\n]*)";
MatchCollection mc = Regex.Matches(test, @"(语言\d+)(?:[^>]*>){2}([^<]+)");
Dictionary<string, string> dict = new Dictionary<string, string>();
foreach (Match m in mc)
{
dict.Add(m.Groups[1].Value, m.Groups[2].Value);
}
Console.WriteLine(dict["语言1"]);
}
难道不是同时?
m.Groups[1].Value 就是 语言1
m.Groups[2].Value 就是 语言1对应的值
不明白你是一定要给这个加个名字还是什么意思
i:忽略大小写
s:.匹配回车换行,表示单行模式
n:取消未命名的分组。