0

Hello I trying to get inner HTML of <p class="mojeip">12.45.45.47</p> in C#, i tried something like>

    Regex emailregex = new Regex(@"(<p class=""mojeip"">)(.*?)(</p>)");

But still cannot get only IP address.

Thanks for any advice.

David
  • 188,958
  • 33
  • 188
  • 262
redrom
  • 11,110
  • 30
  • 151
  • 254

4 Answers4

2

I am sure you are going to get plenty of RegEx solutions. So I want to propose an alternate solution.

I have often used RegEx to extract data from HTML, but more recently I have used the Html Agility Pack and I would highly recommend that if it does not feel like too much of an overkill for your task.

Chris Taylor
  • 51,025
  • 10
  • 74
  • 88
0

If your input string is exactly this complicated and no more, then you could try capturing ([^<]*) instead of (.*?). In real life, you're probably trying to parse HTML or XHTML, and almost certainly want to use a tool other than regex, as per Ignacio's comment above.

jwismar
  • 12,035
  • 3
  • 29
  • 44
0

Regex's are not good for HTML because it is not a regular language.

If you want just this simple action you may use string manipulation and Linq:

   string s = @"<p class=""mojeip"">12.45.45.47</p>";

   var result = s.Skip(s.IndexOf(@"""mojeip""") +
                @"""mojeip""".Length + 1).TakeWhile(c => c != '<').ToArray();

   Console.WriteLine(new string(result));
nan
  • 18,496
  • 7
  • 45
  • 77
0

This query should do it:

(?<=<[^/].*?>).*(?=</.*>) 

Only parse XML/HTML with Regex if invoking tools like LINQ to XML and such is out of the question for performance or whatever reasons tho.

Machinarius
  • 3,467
  • 1
  • 27
  • 50