如何为我的表达定期进行正则表达式匹配? [英] How Do I Give Regular a Regex Match For My Expression?

查看:57
本文介绍了如何为我的表达定期进行正则表达式匹配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

string replacetext="<td title=\"Compatible versions: a29d.3efe\" class=\"critical hastooltip\">"





这是字符串类型变量,它保存XML文档中的值。我的场景是这样的:

此字符串用于匹配标记元素并提取其值。问题是部件兼容版本:a29d.3efe不是常数,因此我无法对其进行硬编码以得到所需的结果,这是我的必需标签。我希望有一个正则表达式或类似的解决方案,它可以识别任何数据,主要是带有特殊符号的字母数字字符,然后我应该能够提取它的值。我该怎么办?

这是我可以分享的代码:



this is string type variable which holds a value from XML document. My scenario is as such:
this string is being used to match the tag element and extract it's value. Problem is that the part Compatible Version:a29d.3efe is not constant and so I can't hardcode it to give desired result,that is my required tag. I wanted to have a regular expression or something similar solution where it can recognise any data, mostly alphanumberic characters with special symbols, and then I should be able to extract it's value. How can i do it?
This is piece fo code that I can share:

if (MFMStatusUrlSettings != null)
                {
                    string replaceText = "<td title=\"Compatible versions: a29d.fe3e\" class=\"critical hastooltip\">";
                    string toGetMatch = MFMStatusUrlSettings.Replace(replaceText, "<td>");
                    MatchCollection Collection = Regex.Matches(toGetMatch, @"<td>(?<Value>.*?)</td>");
                    Firmware_CRC = GetStrValueBetweenTags(Collection[5].ToString(), "<td>", "</td>");

                    deviceInfo.Description = Firmware_CRC;
                }



因此,对于replaceText,我想获取标签数据,而不管兼容版本部分中的内容是什么

XML文档:供参考


So, for replaceText I wanted to get the tag data irrespective of whatever is there in Compatible Version section
XML documents:For reference

Document 1: 
<Status>
<Summary>
<Uptime>05:14:38</Uptime>
<FastWD>00:02:50</FastWD>
<SlowWD>18:45:22</SlowWD>
<Configuration_Download>Success</Configuration_Download>
<Firmware_CRC title="Compatible versions: a29d.4adc" class="critical hastooltip">a29d.3db8</Firmware_CRC>
<Core_Version>19.6</Core_Version>
<App_Version>23.4</App_Version>
<CPLD_Version>4BF44D51</CPLD_Version>
</Summary>
<Detectors>
<state>Operating</state>
<detector>8 channel - Lane agile radar: Operating</detector>
<detector>2 channel - Wired light phase detector: Operating</detector>
<detector>Optical Light Phase Detector: Absent</detector>
<self_test>pass[00]</self_test>
<radar_error_counter>0</radar_error_counter>
</Detectors>
</Status>





文件2:





Document 2:

<Status>
<Summary>
<Uptime>05:58:56</Uptime>
<FastWD>00:02:06</FastWD>
<SlowWD>18:01:04</SlowWD>
<Configuration_Download>Success</Configuration_Download>
<Firmware_CRC title="Compatible versions: a29d.fe3e" class="critical hastooltip">a29d.3db8</Firmware_CRC>
<Core_Version>19.6</Core_Version>
<App_Version>23.4</App_Version>
<CPLD_Version>4BF44D51</CPLD_Version>
</Summary>
<Detectors>
<state>Operating</state>
<detector>8 channel - Lane agile radar: Operating</detector>
<detector>2 channel - Wired light phase detector: Operating</detector>
<detector>Optical Light Phase Detector: Absent</detector>
<self_test>pass[00]</self_test>
<radar_error_counter>0</radar_error_counter>
</Detectors>
</Status>

推荐答案

事实上,它是一个XML文档。然后不要使用RegEx,因为它可能会失败。

使用 LINQ to XML [ ^ ]它是易于搜索和替换XML值。



示例

The fact, that it is a XML document. Then do not use RegEx, as it will likely fail.
Use LINQ to XML[^] it is easy to search and replace XML values.

Example
XElement xml = XElement.Parse(stringWithXml);
XElement findTag = xml.Elements("Firmware_CRC").FirstOrDefault();

if (findTag != null)
{
  string firmwareTitle = findTag.Attribute("title");
  string currentFirmware = findTag.Value;
}





你在这里找到Firmware_CRC title =和Firmware_CRC value



There you have found Firmware_CRC title="" and Firmware_CRC value


这篇关于如何为我的表达定期进行正则表达式匹配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆