all groups > c# > march 2008 >
You're in the

c#

group:

Regex to retain only the HTML body


Regex to retain only the HTML body Karch
3/31/2008 10:39:16 PM
c#: If you run this:

string result = "<html><head></head><body>The body</body></html>";
result = retainBody.Replace(result, "$1");


With the following Regex:

private static readonly Regex retainBody = new
Regex(@"<\s*body[^>]*>(.*)<[\s/]*body[^>]*>", RegexOptions.Compiled |
RegexOptions.IgnoreCase | RegexOptions.Singleline);


You get this as the return:

<html><head></head>The body</html>

I want this instead:

The body

Re: Regex to retain only the HTML body Nikola Stjelja
4/1/2008 11:43:19 AM
[quoted text, click to view]
Try this

string result = "<html><head></head><body>The body</body></html>";
Regex reg = new
Regex(@"<\s*body[^>]*>(?<body>(.*))<[\s/]*body[^>]*>");
Match body=reg.Match(result);
AddThis Social Bookmark Button