昨天未解决的问题今天继续

「已注销」 2008-10-09 09:03:03
昨日提问未解决今日继续题目如下:
string strHtml= "<html><head></head><body><divid="content"><div></div>
</div>
<div></div></body></html>";
以上是一个字符串!需要一正则来匹配红色部分(即<div id="content"><div></div></div>)。
请注意,在id="content"的div中可能还有很多div(可以设想这些div是成对出现的不必考虑异常情况),
...全文
277 22 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
「已注销」 2008-10-09
  • 打赏
  • 举报
回复
「已注销」 2008-10-09
  • 打赏
  • 举报
回复
英文版已下载。
中文版太难找了,找到后我会贴出来!o(∩_∩)o...支持正版
wuyi8808 2008-10-09
  • 打赏
  • 举报
回复
「已注销」 2008-10-09
  • 打赏
  • 举报
回复
恩,谢谢各位大侠热情帮助!
特别感谢ojlovecd wuyi8808(排名不分先后o(∩_∩)o...)
谢谢showbo另一种思路!
结贴给分!
我姓区不姓区 2008-10-09
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 wuyi8808 的回复:]
ojlovecd正则中的
<div\s*id="content"\s*>
第一个\s*应该改为\s+,div和id之间至少要有一个空格字符。
[/Quote]
是的,元素名和属性之间必须要有空格的,不小心被lz误导了
紫气东来_999 2008-10-09
  • 打赏
  • 举报
回复
学习。
wuyi8808 2008-10-09
  • 打赏
  • 举报
回复
ojlovecd正则中的
<div\s*id="content"\s*>
第一个\s*应该改为\s+,div和id之间至少要有一个空格字符。
我姓区不姓区 2008-10-09
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 wuyi8808 的回复:]
C# codeLZ串有问题:<divid="content">要改为:<div id="content">div和id中间至少一个空格字符的,因为我的正则是:<div\s+id="content">\s+代表至少一个空格字符。
[/Quote]
确实是,我没看清楚,不好意思
anlaetion 2008-10-09
  • 打赏
  • 举报
回复
学习学习...正则很难看懂啊
wuyi8808 2008-10-09
  • 打赏
  • 举报
回复
LZ串有问题:
<divid="content">
要改为:
<div id="content">

div和id中间至少一个空格字符的,因为我的正则是:
<div\s+id="content">
\s+代表至少一个空格字符。
我姓区不姓区 2008-10-09
  • 打赏
  • 举报
回复

static void Main(string[] args)
{
string str = @"<html> <head> </head> <body> <divid=""content""> <div><div></div><div><div></div></div></div>
</div> <div> </div> </body> </html>";
Match match = Regex.Match(str, @"<div\s*id=""content""\s*>(?<text>[\s\S]*)</div>");
if (match.Success)
{
string result = GetContent(match.Value, match.Groups["text"].Value);
Console.WriteLine(result);
}
static string GetContent(string match, string content)
{
if (Regex.Matches(content, "<div>").Count != Regex.Matches(content, "</div>").Count)
{
string sub = match.Substring(0, match.LastIndexOf("</div>"));
match = GetContent(sub, Regex.Match(sub, @"<div\s*id=""content""\s*>(?<text>[\s\S]*)</div>").Groups["text"].Value);
}
else if (content.LastIndexOf("<div>") > content.LastIndexOf("</div>"))
{
string sub = match.Substring(0, match.LastIndexOf("<div>"));
match = GetContent(sub, Regex.Match(sub, @"<div\s*id=""content""\s*>(?<text>[\s\S]*)</div>").Groups["text"].Value);
}
return match;
}


输出
<divid="content"> <div><div></div><div><div></div></div></div>
</div>
我姓区不姓区 2008-10-09
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 wuyi8808 的回复:]
用平衡组:
C# codeusingSystem;usingSystem.Text.RegularExpressions;classTest
{staticvoidMain()
{strings=Console.In.ReadToEnd();stringpattern=@"(?six)<div\s+id=""content"">
(?'MyCont'
(?>
(?!<div\b|</div>).
|
<div(?:\s+(?:""[^""]*""|'[^']*'|[^""'>])*)?>(?'div')
|
</div>(?'-div')
)*
(?(div)(?!))
)
</div>";foreach(Match minRege…
[/Quote]
空军这个值得学习,不过似乎有些问题,像下面这个就没有匹配出来
<html> <head> </head> <body> <divid=""content""> <div><div></div><div><div></div></div></div>
</div> <div> </div> </body> </html>
wuyi8808 2008-10-09
  • 打赏
  • 举报
回复
<div(?:\s+(?:""[^""]*""|'[^']*'|[^""'>])*)?>
上面这个式了保证了正确地排除引号中的HTML标记,如<div>之类,也就是说引号里的<div>不算<div>:
<div comment="this is <div> in commment">
lyljr08 2008-10-09
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 fengylm 的回复:]
额。怎么感觉正则表达式和天书一样
[/Quote]

同感

XmlDocument好点
wuyi8808 2008-10-09
  • 打赏
  • 举报
回复
using System;
using System.Text.RegularExpressions;

class Test
{
static void Main()
{
string s = @" <html> <head> </head> <body> <div id=""content""> <div> </div>
<div comment=""this is <div> in commment""></div>
</div> <div> </div> </body> </html>";
string pattern = @"(?six)<div\s+id=""content"">
(
(?>
(?!<div\b|</div>).
|
<div(?:\s+(?:""[^""]*""|'[^']*'|[^""'>])*)?>(?'div')
|
</div>(?'-div')
)*
(?(div)(?!))
)
</div>";
foreach (Match m in Regex.Matches(s, pattern))
{
Console.WriteLine(m.Value);
}
}
}
/* 程序输出:
<div id="content"> <div> </div>
<div comment="this is <div> in commment"></div>
</div>
*/
fengylm 2008-10-09
  • 打赏
  • 举报
回复
额。怎么感觉正则表达式和天书一样
  • 打赏
  • 举报
回复
XmlDocument吧,正则太麻烦了

using System.Xml;
using System;
public class t
{
public static void Main(string[] args)
{
string div = "<div><div id=\"content\"><div><div></div></div></div><div></div></div>";
XmlDocument doc = new XmlDocument();
doc.LoadXml(div);
Console.WriteLine(doc.InnerXml);
XmlNodeList nl = doc.GetElementsByTagName("div");
foreach (XmlNode n in nl)
{
if (n.Attributes["id"]!=null&&n.Attributes["id"].Value == "content")
{
Console.WriteLine(n.InnerXml);
break;
}
}
}
}
wuyi8808 2008-10-09
  • 打赏
  • 举报
回复
用平衡组:
using System;
using System.Text.RegularExpressions;

class Test
{
static void Main()
{
string s = Console.In.ReadToEnd();
string pattern = @"(?six)<div\s+id=""content"">
(?'MyCont'
(?>
(?!<div\b|</div>).
|
<div(?:\s+(?:""[^""]*""|'[^']*'|[^""'>])*)?>(?'div')
|
</div>(?'-div')
)*
(?(div)(?!))
)
</div>";
foreach (Match m in Regex.Matches(s, pattern))
{
Console.WriteLine(m.Value);
}
}
}
troy-zhou 2008-10-09
  • 打赏
  • 举报
回复
没明白你的意思!!!!!
twtetg 2008-10-09
  • 打赏
  • 举报
回复
坐在地板上学习了
加载更多回复(2)

62,243

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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