Click here to Skip to main content
13,043,491 members (55,192 online)
Rate this:
Please Sign up or sign in to vote.
See more:
hi experts,

i have a string containing Html code, now i need to remove one div from the string.

string Html="<div id="header">having some htmlcode</div> <div id="body">having some htmlcode</div> <div id="footer">having some htmlcode</div>";

now i need to remove one div part from the above string.

Sandeep Chowdary
Posted 4-Feb-13 18:58pm
Updated 4-Feb-13 19:16pm

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

In computing, a regular expression is a specific pattern that provides concise and flexible means to "match" (specify and recognize) strings of text, such as particular characters, words, or patterns of characters. Common abbreviations for "regular expression" include regex and regexp.

Here's a regex example with your string:
the only thing you have to edit is the RegexPattern var
using System;
using System.Text.RegularExpressions;
class Program
    static void Main()
        // This is the input string we are replacing parts from.
        string input = "<div id="header"></div> <div id="body"></div> <div id="footer"></div>";
        // Use Regex.Replace to replace the pattern in the input.
        // Here you enter your pattern with the div or anything else you wish to remove.
        string RegexPattern = "body";  
        string output = Regex.Replace(input, RegexPattern , "");
        // Write the output.

See example of using regex in javascript[^]

Kishor Deshpande 5-Feb-13 1:18am
My 5.
Edo Tzumer 5-Feb-13 1:19am
Thank you Kishor
Edo Tzumer 5-Feb-13 1:22am
Hello Sandeep,

You need to edit the RegexPattern string. in the example this is what it is set to replace
Are you at all familiar with Regex patterns?
sandeep nagabhairava 5-Feb-13 1:43am
ok Edo thanks, i am not familiar with Regex patterns.
Edo Tzumer 5-Feb-13 1:54am
They are very useful and I suggest you get into it.
In any case,

Try this as a pattern:
string RegexPattern = "<div\b[^>]*>(.*?)<\/div>";
Edo Tzumer 5-Feb-13 1:58am
by the way you said you need to remove one div,
Which one do you want to remove?
sandeep nagabhairava 5-Feb-13 2:19am
<div id="CommonHeader"> some htmlcode </div> i need to remove this from string

thanks for your support
Edo Tzumer 5-Feb-13 2:24am
Then this is your regex:
string RegexPattern = "<div\b[^>]*CommonHeader[^>]*>(.*?)<\/div>";
Edo Tzumer 5-Feb-13 2:38am
oh try using @:

string RegexPattern = @"<div\b[^>]*CommonHeader[^>]*>(.*?)<\/div>";
07405 11-Feb-13 4:04am
Hi Edo,
I too need the same that i want to remove div from string which is from checkeditor. i tried @"<div\b[^>]*CommonHeader[^>]*>(.*?)<\/div>"; it worked. but html from CkEditor it doesn,t work out. help me in this aspect...
Edo Tzumer 11-Feb-13 4:27am
Please clarify, I'm not sure what you need.
Maybe post another question with your text, and needs.
07405 11-Feb-13 4:37am
do u know CK Editor?
Edo Tzumer 11-Feb-13 4:47am
Show us the String and the pattern you want to catch and I'll give it a shot.
07405 11-Feb-13 5:05am
hi my string was like this, now i want to remove 1st div from this
"<div id=\"commonHeader\">\r\n\t<p>\r\n\t\tThis is Comman Header</p>\r\n</div>\r\n<p>\r\n\tiCloudBiz is the best way to connect with your businesses and make it easy for making appointments with small businesses. Join and start using our free services! Use our recommend page to all the businesses you need to connect with.</p>\r\n<div id=\"CommonFooter\">\r\n\tthis is commonFooter</div>\r\n"
07405 11-Feb-13 5:09am
your given string RegexPattern = @"<div\b[^>]*CommonHeader[^>]*>(.*?)<\/div>";
works awesomely for "<div id="CommonHeader"> some htmlcode </div>
<div id="body"> html code</div>".
but not the above pattern
Edo Tzumer 11-Feb-13 5:15am
And what text to you want it to work with?
You can change the "CommonHeader" in the pattern to any id of the div that you wish, it will catch the matching div accordingly.
07405 11-Feb-13 5:52am
div with \n \t \r are not catches by the RegaxPattern
thnx for reply.
Edo Tzumer 11-Feb-13 7:22am
What technology are you using for the regex?
The "(.*)?" in the middle of the div should catch any char including \n\r\t
07405 11-Feb-13 7:18am
the above one catches only div, it dos't catch content

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.170713.1 | Last Updated 5 Feb 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100