473,569 Members | 2,611 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Regex to retain only the HTML body

If you run this:

string result = "<html><hea d></head><body>The body</body></html>";
result = retainBody.Repl ace(result, "$1");
With the following Regex:

private static readonly Regex retainBody = new
Regex(@"<\s*bod y[^>]*>(.*)<[\s/]*body[^>]*>", RegexOptions.Co mpiled |
RegexOptions.Ig noreCase | RegexOptions.Si ngleline);
You get this as the return:

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

I want this instead:

The body
Apr 1 '08 #1
1 8791
Karch wrote:
If you run this:

string result = "<html><hea d></head><body>The body</body></html>";
result = retainBody.Repl ace(result, "$1");
With the following Regex:

private static readonly Regex retainBody = new
Regex(@"<\s*bod y[^>]*>(.*)<[\s/]*body[^>]*>", RegexOptions.Co mpiled |
RegexOptions.Ig noreCase | RegexOptions.Si ngleline);
You get this as the return:

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

I want this instead:

The body

Try this

string result = "<html><hea d></head><body>The body</body></html>";
Regex reg = new
Regex(@"<\s*bod y[^>]*>(?<body>(.*)) <[\s/]*body[^>]*>");
Match body=reg.Match( result);
Console.WriteLi ne(body.Groups["body"].ToString());
Apr 1 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
5177
by: Markus Ernst | last post by:
Hello I have a regex problem, spent about 7 hours on this now, but I don't find the answer in the manual and googling, though I think this must have been discussed before. I try to simply extract the title and meta tags of a valid HTML page as an array: function extract_html($filename)
8
1923
by: Mike Gifford | last post by:
Hello, I've got a number of html fragments (in a template file) which are bundled together in a cms. I'm sure that there's a way through javascript to convert a piece of code like this: <!-- START BE_sectionNew-htmlarea2.tpl -->
1
4556
by: jason | last post by:
I have exhausted all resources, so perhaps someone out there can help. I have a 8MB string (that represents an html document) and I am trying to run a couple of regular expressions on this string. I receive a System.OutOfMemoryException. Why am I getting this error? What can I do to fix this problem? This code works fine for smaller...
1
3377
by: Mark | last post by:
Hi, I've seen some postings on this but not exactly relating to this posting. I'm reading in a large mail message as a string. In the string is an xml attachment that I need to parse out and remove from the message once processed. I have to do this as a string and not using any CDO libraries. My problem is that there's normally a large pdf...
6
1182
by: Julia | last post by:
assuming I have template HTML file which contains property names to evaluate at run time <HTML> <BODY><%PropertyName1%> <DIV ID="<%PropertyName2%>"> </HTML> How do I find all matches for <%...%> ,i dont know which pattern to use.
1
3412
by: kevin | last post by:
I am trying to strip the outermost html tag by capturing this tag with regex and then using the string replace function to replace it with an empty string. while stepping through the code, RegEx returns the entire input string although testing this in The Regulator returns just what I want. What am I doing wrong here?...
2
1516
by: tshad | last post by:
I am trying to get a regular expression to work and I keep getting the following error: Compiler Error Message: BC30469: Reference to a non-shared member requires an object reference. The page is: ********************************************************************************** <%@ Page Language="VB" trace="true" debug="true"...
4
1750
by: dkacher | last post by:
Hello - I'm looking for a way to retain newlines when using XSL to copy the content of an XML attribute. I have this XML input: <?xml version="1.0"?> <A > <B x = "stuff" y = "line 1 line 2" /> </A>
2
12223
by: qwedster | last post by:
Folks! The following is a "Hello World" kind of code for ViewState. I just want to know how to retain the ViewState 1) while Page Refresh when using UpdatePanel and also 2) While I reverting back to the page after round trip when using UpdatePanel? In the following code snippet the ViewState is killed when I click page refresh or when I go...
0
7700
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7614
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7924
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7676
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5513
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3642
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1221
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
938
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.