Click here to Skip to main content
15,892,797 members

How Do I Give Regular a Regex Match For My Expression?

Anshumaan Chaturvedi asked:

Open original thread
C#
string replacetext="<td title=\"Compatible versions: a29d.3efe\" class=\"critical hastooltip\">"


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:
C#
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;
                }

So, for replaceText I wanted to get the tag data irrespective of whatever is there in Compatible Version section
XML documents:For reference
HTML
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>


Document 2:

HTML
<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>
Tags: C#, Regular Expressions

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900