请教一正则表示式

eFrey 2011-01-23 05:05:34
示例字符串:

<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>


请教这个正则表示式该如何取?
...全文
105 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
eFrey 2011-01-24
  • 打赏
  • 举报
回复
非常感谢,
另外想请教一下,如果要求不能包含"<img",但同时需要包含keyword1和keyword2两个词,该如何写呢
兔子-顾问 2011-01-23
  • 打赏
  • 举报
回复
换行。。。

MatchCollection mc = Regex.Matches(yourStr,@"(?is)<img(?!.+<img).+?<span></span>");
foreach(Match m in mc)
{
m.Value;//你要的
}
兔子-顾问 2011-01-23
  • 打赏
  • 举报
回复
MatchCollection mc = Regex.Matches(yourStr,@"<img(?!.+<img).+?<span></span>");
foreach(Match m in mc)
{
m.Value;//你要的
}
wuyq11 2011-01-23
  • 打赏
  • 举报
回复
<img ..> .. <spna></spam>中内容,中间不需要img就用
MatchCollection mc = Regex.Matches(str, @"<img((?!img)[\s\S])+?<span></span>");

110,538

社区成员

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

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

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