Reputation: 84
I need to extract the $value from the given piece of string .
string text = "<h2 class="knownclass unknownclass1 unknownclass2" title="Example title>$Value </h2>"
Using the code -:
Match m2 = Regex.Match(text, @"<h2 class=""knownclass(.*)</h2>", RegexOptions.IgnoreCase);
It gets me the full value -: unknownclass1 unknownclass2" title="Example title>$Value .But I just need the $value part. Please tell me .Thanks in advance.
Upvotes: 1
Views: 220
Reputation: 3625
If its always the same pattern of your string, you can consider this:
string text = "<h2 class=\"knownclass unknownclass1 unknownclass2\" title=\"Example title>$Value </h2>";
string result = "";
Regex test = new Regex(@"\<.*?\>(.*?)\</h2\>");
MatchCollection matchlist = test.Matches(text);
if (matchlist.Count > 0)
{
for (int i = 0; i < matchlist.Count; i++)
{
result = matchlist[i].Groups[1].ToString();
}
}
But if you are working with XML files or HTML files, I recommend you use XmlTextReader for XML and HtmlAgilityPack for HTML
http://msdn.microsoft.com/en-us/library/system.xml.xmltextreader.aspx
http://htmlagilitypack.codeplex.com/
hope it helps!
Upvotes: 0
Reputation: 67898
Assuming the string always follows this format, consider the following code:
var index = text.IndexOf(">");
text.Substring(index + 1, text.IndexOf("<", index));
Upvotes: 1
Reputation: 19203
As had been said multiple time, using a Regex for parsing HTML or XML is bad. Ignoring that, you are capturing too much. Here is an alternative Regex that should work.
@"<h2 class=""knownclass[^""]*"">(.*)</h2>"
Upvotes: 0