Reputation: 209
I'm having something like that:
{
Regex regex = new Regex(@"expression here");
foreach (Match match in regex.Matches(textBoxResponse.Text))
{
MessageBox.Show(match.Value.ToString());
}
}
What expression should I use to get only EXAMPLE from this response?
<a href="spieler.php?uid=xxx">EXAMPLE</a>
Upvotes: 0
Views: 1349
Reputation: 3365
Consider using the htmlagilitypack for this.
Edit:
Updated the example based on Casimir et Hippolyte's suggestion A quick introduction to XPATH can be read here: http://zvon.org/xxl/XPathTutorial/General/examples.html
The following code finds all the hyperlinks on a page. Reference:http://htmlagilitypack.codeplex.com/wikipage?title=Examples
var doc = new HtmlDocument(); //HtmlDocument class is part of the htmlagilitypack
doc.LoadHtml(@"<html><body><a href='spieler.php?uid=xxx'>EXAMPLE</a></body></html>");
foreach(var linkText in doc.DocumentNode.SelectNodes("//a/text()"))
{
Console.WriteLine(linkText.InnerText);
}
Upvotes: 3
Reputation: 15334
var html = @"<a href=""spieler.php?uid=xxx"">EXAMPLE</a>";
var matches = Regex.Matches(html, @"<a\ href=""spieler\.php\?uid=[^""]*"">([^(</a>)]*)</a>");
foreach (Match match in matches)
{
Console.WriteLine(match.Groups[1].Value);
}
Hope that helps :)
Or if you wish to use Linq.
var html = @"<a href=""spieler.php?uid=xxx"">EXAMPLE</a>";
var matches = Regex.Matches(html, @"<a\ href=""spieler\.php\?uid=[^""]*"">([^(</a>)]*)</a>");
var examples = (from Match match in matches select match.Groups[1].Value).ToList();
Upvotes: 0
Reputation: 1245
this should work, it will look at text between > and < / a>
(?<=>)(.*?)(?=</a>)
but as stated in the comments i would not advise to parse html with regex
Upvotes: 1