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.
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.
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.
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.
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));
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.