求一个正则表达式的书写

guyanlj 2010-12-17 04:19:15
现在一段HTML的字符串如下:
<P STYLE=\"margin:0 0 0 0;font-family:宋?体??;font-size:13.3333333333333;\"> <SPAN>c</SPAN><SPAN STYLE=\"text-decoration:underline;\">o</SPAN>
<SPAN>mmon</SPAN></P>


我现在主要从中提取
<SPAN STYLE=\"text-decoration:underline;\">o</SPAN>
出来,我暂用的正则表达式是
<SPAN.*(?=text-decoration:underline;)(.|\n)*?</SPAN>
如果上述HTML代码是
<P STYLE=\"margin:0 0 0 0;font-family:宋?体??;font-size:13.3333333333333;\">
<SPAN>c</SPAN>
<SPAN STYLE=\"text-decoration:underline;\">o</SPAN>
<SPAN>mmon</SPAN></P>
分行来显示,则可以满足我的效果。但如果是HTML只放在一个字符串中时,则无法获取,求如何获取
<SPAN STYLE=\"text-decoration:underline;\">o</SPAN>

的正则表达式书写方法?
...全文
77 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
-过客- 2010-12-17
  • 打赏
  • 举报
回复
(.|\n) 是所有可匹配任意字符的写法里效率最低的

(?s). 是效率最高的
-过客- 2010-12-17
  • 打赏
  • 举报
回复
4楼多了个字符

            Regex reg = new Regex(@"(?is)<SPAN[^>]*?STYLE=""text-decoration:underline;""[^>]*>.*?</SPAN>");
MatchCollection mc = reg.Matches(yourStr);
foreach (Match m in mc)
{
richTextBox2.Text += m.Value + "\n";
}
guyanlj 2010-12-17
  • 打赏
  • 举报
回复
OK,非常感谢,第一个可以了,第二个第三个暂时还没出来结果,但也给分,结贴
-过客- 2010-12-17
  • 打赏
  • 举报
回复
try...

Regex reg = new Regex(@"(?is)<SPAN[^>]*?STYLE=""text-decoration:underline;""[^>]*>>.*?</SPAN>");
q107770540 2010-12-17
  • 打赏
  • 举报
回复

void Main()
{
string html="<P STYLE=\"margin:0 0 0 0;font-family:宋?体??;font-size:13.3333333333333;\"> <SPAN>c</SPAN><SPAN STYLE=\"text-decoration:underline;\">o</SPAN><SPAN>mmon</SPAN></P>";

foreach(Match m in Regex.Matches(html,@"(?is)<span[^>]*style=(['""]?)text-decoration:underline;\1>.*?</span>"))
{
Console.WriteLine(m.Value);
}
}

/*
<SPAN STYLE="text-decoration:underline;">o</SPAN>
*/
不懂装懂 2010-12-17
  • 打赏
  • 举报
回复
<SPAN[^>]*(?=text-decoration:underline;)(.|\n)*?</SPAN>
在你原来的基础上改的
guyanlj 2010-12-17
  • 打赏
  • 举报
回复
补充一句,用<SPAN.*(?=text-decoration:underline;)(.|\n)*?</SPAN>
正则表达式的话,出现在结果是
<SPAN>c</SPAN><SPAN STYLE=\"text-decoration:underline;\">o</SPAN>

它把前面不要部分<SPAN>c</SPAN>也加了进来,这就不是我要的效果了。

62,039

社区成员

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

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

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

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