C# 中的正则表达式条件问题

发布于 2024-11-14 17:33:27 字数 558 浏览 3 评论 0原文

我有一些内容嵌套在 span 标签中。其中一些有我需要提取的详细信息,有些则不需要。我不知道如何检查两个选项并提取正确的数据。这些组重复。例如:

<span name="foo">
    <span name="bar">
        Missing Data
    </span>
</span>
<span name="foo">
    <span name="bar">
        <span name="detail1">first detail</span>
        <span name="detail2">second detail</span>
    </span>
</span>

我必须单独捕获详细信息(如果存在),否则我需要在循环遍历 matchcollection 时将程序中的字符串中的这些值设置为 null,因此我的代码需要将 strDetail1 和 strDetail2 设置为“”或值“第一个细节”和“第二个细节”如果有意义的话。

I have some content nested in span tags. Some of them have details I need to pull, and some do not. I can't figure out how to check for two options and pull the proper data. These groups repeat. For example:

<span name="foo">
    <span name="bar">
        Missing Data
    </span>
</span>
<span name="foo">
    <span name="bar">
        <span name="detail1">first detail</span>
        <span name="detail2">second detail</span>
    </span>
</span>

I have to capture the details individually if they are there, otherwise I need to set those values to null in the strings in my program when looping through the matchcollection so my code needs to set strDetail1 and strDetail2 to "" or the values "first detail" and "second detail" if that makes sense.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

徒留西风 2024-11-21 17:33:27

我建议使用 XPath 来解析值。对于解析 xml 结构,这比正则表达式更可靠。

var xml = @"
    <root>
    <span name=""foo"">
        <span name=""bar"">
            Missing Data
        </span>
    </span>
    <span name=""foo"">
        <span name=""bar"">
            <span name=""detail1"">first detail</span>
            <span name=""detail2"">second detail</span>
        </span>
    </span>
    </root>
";

var document = XDocument.Parse(xml);
var details = document.XPathSelectElements("//span[@name='foo']/span[@name='bar']/span[starts-with(@name,'detail')]")
    .Select(arg => arg.Value)
    .ToList();

或 LINQ-to-XML

var details = document
    .Descendants("span").Where(arg => arg.Attribute("name").Value == "foo")
    .Elements("span").Where(arg => arg.Attribute("name").Value == "bar")
    .Elements("span").Where(arg => arg.Attribute("name").Value.StartsWith("detail"))
    .Select(arg => arg.Value)
    .ToList();

[编辑] 我可能会误解这个问题。似乎您还想替换或填充一些值。只要您有 XDocument,您就可以使用上述方法来执行此操作。例如,此代码将清除 detail1detail2 元素的值:

var detailNodes = document.XPathSelectElements("//span[@name='foo']/span[@name='bar']/span[starts-with(@name,'detail')]")
    .ToList();

detailNodes[0].Value = string.Empty;
detailNodes[1].Value = string.Empty;

var newXml = document.ToString();

[Edit]

如何添加元素:

var elementsWithMissingDetals = document
    .XPathSelectElements("//span[@name='foo']/span[@name='bar' and count(*)=0]")
    .ToList();

foreach (var elementsWithMissingDetal in elementsWithMissingDetals)
{
    elementsWithMissingDetal.Add(
        new XElement("span", "first detail", new XAttribute("name", "detail1")));
    elementsWithMissingDetal.Add(
        new XElement("span", "second detail", new XAttribute("name", "detail2")));
}

var newXml = document.ToString();

I suggest using XPath to parse values. For parsing xml structure this will be more reliable than Regex.

var xml = @"
    <root>
    <span name=""foo"">
        <span name=""bar"">
            Missing Data
        </span>
    </span>
    <span name=""foo"">
        <span name=""bar"">
            <span name=""detail1"">first detail</span>
            <span name=""detail2"">second detail</span>
        </span>
    </span>
    </root>
";

var document = XDocument.Parse(xml);
var details = document.XPathSelectElements("//span[@name='foo']/span[@name='bar']/span[starts-with(@name,'detail')]")
    .Select(arg => arg.Value)
    .ToList();

or LINQ-to-XML

var details = document
    .Descendants("span").Where(arg => arg.Attribute("name").Value == "foo")
    .Elements("span").Where(arg => arg.Attribute("name").Value == "bar")
    .Elements("span").Where(arg => arg.Attribute("name").Value.StartsWith("detail"))
    .Select(arg => arg.Value)
    .ToList();

[Edit] I might misunderstand the question. Seems like you also want to replace or fill some values. You can do this with above-mentioned approach as long as you have XDocument. For example this code will clear values of the detail1 and detail2 elements:

var detailNodes = document.XPathSelectElements("//span[@name='foo']/span[@name='bar']/span[starts-with(@name,'detail')]")
    .ToList();

detailNodes[0].Value = string.Empty;
detailNodes[1].Value = string.Empty;

var newXml = document.ToString();

[Edit]

How to add an element:

var elementsWithMissingDetals = document
    .XPathSelectElements("//span[@name='foo']/span[@name='bar' and count(*)=0]")
    .ToList();

foreach (var elementsWithMissingDetal in elementsWithMissingDetals)
{
    elementsWithMissingDetal.Add(
        new XElement("span", "first detail", new XAttribute("name", "detail1")));
    elementsWithMissingDetal.Add(
        new XElement("span", "second detail", new XAttribute("name", "detail2")));
}

var newXml = document.ToString();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文