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
  • 208,112
  • 36
  • 198
  • 279
redrom
  • 11,502
  • 31
  • 157
  • 264

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
  • 52,623
  • 10
  • 78
  • 89
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,164
  • 3
  • 32
  • 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
  • 19,595
  • 7
  • 48
  • 80
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,637
  • 3
  • 30
  • 53