100分 百分 坐等正则表达式为什么不循环内有代码,感谢!感谢

.Net技术 码拜 8年前 (2016-05-03) 895次浏览
string htmval = “”;
string htm = @”<span style=””color: #00000;font-size:15px;font-weight:bold;””>本人是内容1</span>
<span style=””color: #000111;font-size:12px;””>本人是内容2</span>
<span style=””font-size:16px;font-weight:bold;””>本人是内容3</span>”;
var pattern = @”<span[^>]*style=””(?<style>[^>]*)””[^>]*>(?<txt>.*)</span>”;
Regex reg = new Regex(pattern, RegexOptions.None | RegexOptions.IgnoreCase | RegexOptions.Multiline);
MatchCollection mc = reg.Matches(htm);
foreach (Match mcItem in mc)
{
var style = mcItem.Groups[“style”].Value;
var txt = mcItem.Groups[“txt”].Value;
htmval = htmval + “[WITBANKSPAN_” + style + “]” + txt + “[/WITBANKSPAN]”;
}
如上代码,为什么只循环一次希望高手帮本人改一下谢过!
解决方案

100

var pattern = @"<span[^>]*style=""(?<style>[^>]*)""[^>]*>(?<txt>.*?)</span>";

你用了贪婪匹配,在txt部分加个?就可以了


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明100分 百分 坐等正则表达式为什么不循环内有代码,感谢!感谢
喜欢 (0)
[1034331897@qq.com]
分享 (0)