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]”;
}
如上代码,为什么只循环一次希望高手帮本人改一下谢过!
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部分加个?就可以了