user3014282
user3014282

Reputation: 209

c# regex, text between tags

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

Answers (4)

gpmurthy
gpmurthy

Reputation: 2427

Consider the following Regex...

(?<=\>).*?(?=<)

Good Luck!

Upvotes: 1

NoviceProgrammer
NoviceProgrammer

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

Aydin
Aydin

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

jonas vermeulen
jonas vermeulen

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

Related Questions