请教一个关于匹配的正则表达式

eFrey 2011-01-23 06:22:19
示例字符串:

<img width=100 src="files.jpg"> This is a test page <img width=100 src="files2.jpg"> hello world, html test <img width=100 src="ffword.jpg"> from here, wodd. <span></span>

This is a test page <img width=100 src="files2.jpg"> hello world, html test <img width=100 src="ffword.jpg"> from here, wodd. <span></span>


取所有以"<img"开始和以"<span></span>"结束之间的字符串,但取到的字符串中只能含有一个"<img"

例如前面的字符串中需要取到"<img width=100 src="files2.jpg"> hello world, html test <img width=100 src="ffword.jpg"> from here, wodd. <span></span>"
而不是
<img width=100 src="files.jpg"> This is a test page <img width=100 src="files2.jpg"> hello world, html test <img width=100 src="ffword.jpg"> from here, wodd. <span></span>


请教这个正则表示式该如何取?
...全文
112 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
eFrey 2011-01-24
  • 打赏
  • 举报
回复
怎么分组呢?

不能包含"<img",但同时需要包含keyword1和keyword2.
Jack2013tong 2011-01-24
  • 打赏
  • 举报
回复
楼上的可以,用正则分组
eFrey 2011-01-24
  • 打赏
  • 举报
回复
1 楼的也是对的,而且更通用一些。

另外想问一下,如果匹配出的字符串中同时需要有"keyword1"和"keyword2",一楼的正则表达式<img((?!img)[\s\S])+?<span></span>该如何修改呢?
lovehong123 2011-01-23
  • 打赏
  • 举报
回复
string str="<img width=100 src=\"files.jpg\"> This is a test page <img width=100 src=\"files2.jpg\"> hello world, html test <img width=100 src=\"ffword.jpg\"> from here, wodd. <span></span>";
Regex reg = new Regex(@"(?is)<img\swidth=100[^>]*>(.*?)<span></span>");
MatchCollection mc = reg.Matches(str);
foreach (Match m in mc)
{
Console.Write(m.Groups[1].Value + "\n");
}
This is a test page <img width=100 src="files2.jpg"> hello world, html test <img width=100 src="ffword.jpg"> from here, wodd.


二楼的这个是正确的,我试的!
eFrey 2011-01-23
  • 打赏
  • 举报
回复
2楼的是正解
wuyq11 2011-01-23
  • 打赏
  • 举报
回复
string str="<img width=100 src=\"files.jpg\"> This is a test page <img width=100 src=\"files2.jpg\"> hello world, html test <img width=100 src=\"ffword.jpg\"> from here, wodd. <span></span>";
Regex reg = new Regex(@"(?is)<img\swidth=100[^>]*>(.*?)<span></span>");
MatchCollection mc = reg.Matches(str);
foreach (Match m in mc)
{
Console.Write(m.Groups[1].Value + "\n");
}
This is a test page <img width=100 src="files2.jpg"> hello world, html test <img width=100 src="ffword.jpg"> from here, wodd.
Mr-Jee 2011-01-23
  • 打赏
  • 举报
回复
string str = @"<img width=100 src=""files.jpg""> This is a test page <img width=100 src=""files2.jpg""> hello world, html test <img width=100 src=""ffword.jpg""> from here, wodd. <span></span>

This is a test page <img width=100 src=""files2.jpg""> hello world, html test <img width=100 src=""ffword.jpg""> from here, wodd. <span></span>";
MatchCollection mc = Regex.Matches(str, @"<img((?!img)[\s\S])+?<span></span>");
foreach (Match m in mc)
{
Console.WriteLine(m);
}

62,243

社区成员

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

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

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

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