Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 组合嵌套的跨距标记_C#_Html_Asp.net_Regex - Fatal编程技术网

C# 组合嵌套的跨距标记

C# 组合嵌套的跨距标记,c#,html,asp.net,regex,C#,Html,Asp.net,Regex,有人能帮我组合嵌套的span标记吗 我有一些生成的HTML,我试图整理,我有麻烦让这一点工作。 HTML示例: <p> <strong> <span style="font-family:arial,sans-serif"> <span style="color:black"> <span style="font-size:medium">HELLO</span> <

有人能帮我组合嵌套的span标记吗

我有一些生成的HTML,我试图整理,我有麻烦让这一点工作。 HTML示例:

<p>
  <strong>
    <span style="font-family:arial,sans-serif">
      <span style="color:black">
        <span style="font-size:medium">HELLO</span>
      </span>
    </span>
  </strong>
</p>
我要做的是将span标记组合成具有组合样式的一个,因此输出为:

<p>
  <strong>
    <span style="font-family:arial,sans-serif;color:black;font-size:medium">HELLO</span>
  </strong>
</p>
我正在asp.NET4.0中使用C

谢谢, 迈克应该放在标签后面。还有一个称为字体权重的样式属性,可以将其设置为粗体

<p>
    <span style="font-family:arial,sans-serif;color:black;font-size:medium;font-weight:bold">HELLO</span>
</p>

我提出了这个解决方案,这不是一种单行程序的解决方案,但这里是:假设您在一个名为foo的变量中有HTML文本,那么您可以执行以下操作:

string replacement1 = "\"";
string replacement2 = "</span>";
string pattern = @"(?<=<span style=\")[^\"]+"; //Will match all the style strings
string pattern1 = @"(?<=<span style=)(.|\s)+\"(?=>[^<>].+</span>)"; //Will match from the first " to the last " before HELLO
string pattern2 = @"(</span>\s*)+"; //Will match any number of </span> tags
Regex rgx = new Regex(pattern);
MatchCollection matches = rgx.Matches(foo);
foreach (Match match in matches)
    replacement1 += match.Value + ";"; //Builds the new styles string
replacement1 += "\"";
Regex rgx = new Regex(pattern1);
string result = rgx.Replace(foo, replacement1); //Replace the multiple span style tags with a single one
Regex rgx = new Regex(pattern2); 
string result = rgx.Replace(foo, replacement2); //Replace the multiple closing span tags with a single one
第一次更换后,您应该得到

<p>
  <strong>
    <span style="font-family:arial,sans-serif;color:black;font-size:medium">HELLO</span>
      </span>
    </span>
  </strong>
</p>
第二次更换后:

<p>
  <strong>
    <span style="font-family:arial,sans-serif;color:black;font-size:medium">HELLO</span>
  </strong>
</p>

我无法测试它,它可能有一些打字错误,但它应该工作

您可以使用jQuery获得预期结果:

var css = "";
$("span").each(function (i) {
  css += $(this).attr('style')+';';
});
$("span").children().unwrap('<span>');
$("span").attr('style', css);

下面是我使用名为HTML Agility Pack的HTML解析器1.4.6版编写的解决方案。将此库添加到项目中以使用以下代码

var doc = new HtmlDocument();
doc.LoadHtml(INPUT);
foreach(var currentSpanNode in doc.DocumentNode.SelectNodes("//span")) {
    var parentNode = currentSpanNode.ParentNode;
    if (parentNode.Name != "span") continue;
    MergeStyleValuesLeft(parentNode.Attributes["style"], currentSpanNode.Attributes["style"]);
    parentNode.RemoveChild(currentSpanNode);
    parentNode.AppendChildren(currentSpanNode.ChildNodes);
}

var sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
doc.Save(sw);
此时,新的HTML代码位于StringBuilder对象中。上面的代码使用了一个名为MergeStyleValuesLeft的函数。我这里有一个简单的函数版本。根据您的要求,您可以改进它以处理重复样式

private void MergeStyleValuesLeft(HtmlAttribute leftAttribute, HtmlAttribute rightAttribute) {
    if (leftAttribute == null || rightAttribute == null) return;
    char[] styleSeparators = "; ".ToCharArray();
    string leftValue = leftAttribute.Value.Trim(styleSeparators);
    string rightValue = rightAttribute.Value.Trim(styleSeparators);
    leftAttribute.Value = String.Format("{0};{1}", leftValue, rightValue);
}

对不起,自从我问了这个问题后,我就离开了。与此同时,一位同事看了看,想出了一个解决办法

正如我在上面对Brad评论的那样,我发布的HTML是一个非常精简的示例,下面是我们使用的测试代码的链接

我的同事就是这样做的: 首先找到嵌套的open
String outputHTML;
Regex re = new Regex("<span style=\"(.*?)\">(<span style=\"(.*?)\">)+", RegexOptions.IgnoreCase);
outputHTML = re.Replace(inputHTML, new MatchEvaluator(StyleMerger));

static string StyleMerger(Match regexMatch)
{
    String matchedText = regexMatch.ToString();
    return matchedText.Replace("\"><span style=\"", ";");
}
然后查找并替换嵌套的关闭标记

re = new Regex("</span>(</span>)+", RegexOptions.IgnoreCase);
outputHTML = re.Replace(outputHTML, "</span>");

这就产生了这个HTML

是什么生成了这个HTML?用户控件、类、ASPX页面内的普通输出?@Brad。HTML来自一个旧系统,我们被告知不能更改,上面的示例是一个非常简化的示例,整个输出中将有许多嵌套的跨距。@mbeckish您链接到那里的奇怪帖子,但我想我明白了!这不是他所要求的。你可以把它作为评论。