C# 中的正则表达式条件问题
我有一些内容嵌套在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议使用 XPath 来解析值。对于解析 xml 结构,这比正则表达式更可靠。
或 LINQ-to-XML
[编辑] 我可能会误解这个问题。似乎您还想替换或填充一些值。只要您有
XDocument
,您就可以使用上述方法来执行此操作。例如,此代码将清除detail1
和detail2
元素的值:[Edit]
如何添加元素:
I suggest using XPath to parse values. For parsing xml structure this will be more reliable than Regex.
or LINQ-to-XML
[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 thedetail1
anddetail2
elements:[Edit]
How to add an element: