Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
4.33/5 (3 votes)
See more:
hi experts,

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

Ex:
C#
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.

thanks&Regards
Sandeep Chowdary
Posted
Updated 4-Feb-13 19:16pm
v2

1 solution

Regex!
Quote:
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
C#
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.
        Console.WriteLine(input);
        Console.WriteLine(output);
    }
}

See example of using regex in javascript http://stackoverflow.com/questions/9960421/regular-expression-replace-in-javascript[^]

Cheers,
Edo
 
Share this answer
 
v5
Comments
Kishor Deshpande 5-Feb-13 1:18am    
My 5.
Joezer BH 5-Feb-13 1:19am    
Thank you Kishor
Joezer BH 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.
Joezer BH 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>";

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900