0

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>
3
  • 1
    "EXAMPLE" should work :) Commented Dec 7, 2013 at 11:43
  • 6
    Do not use regex to parse HTML/XML text. See here for one of the most famous questions on SO stackoverflow.com/questions/1732348/… Commented Dec 7, 2013 at 11:43
  • Forgot to add that I'm having lots of lines with different text which I named EXAMPLE :P Commented Dec 7, 2013 at 11:45

4 Answers 4

3

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);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Why not using //a/text() as xpath query?
@CasimiretHippolyte: that is indeed the better way. Updated.
1

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

1 Comment

@user3014282 you should be doing something like Regex regex = new Regex("(?<=>)(.*?)(?=</a>)"); string e = "<a href=\"spieler.php?uid=xxx\">EXAMPLE</a>"; string result = regex.Split(e)[1];.
1

Consider the following Regex...

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

Good Luck!

Comments

0
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();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.