Take the 2-minute tour ×

I have a Html code and I want to Convert it to plain text but keep only colored text tags. for example: when I have below Html:


This is a <b>sample</b> html text.
<p align="center" style="color:#ff9999">this is only a sample<p>
and some other tags...

I want the output:

this is a sample html text.
<#ff9999>this is only a sample<>
and some other tags...
share|improve this question
what is the code you have written to achieve this –  Satya Apr 18 '12 at 6:25
thanx, not specified code. plain text with color name tags. –  Ali.M Apr 18 '12 at 6:29
but you need to write some code to parse the code , right ? –  Satya Apr 18 '12 at 6:31
@Ali.M - Satya means what are you using to even try to pull this off? Jquery? PHP? RegEx? –  Anthony Apr 18 '12 at 7:00
Also, you want to replace <p> by <>, but <b> by nothing? –  Mr Lister Apr 18 '12 at 7:13
show 1 more comment

2 Answers

up vote 1 down vote accepted

I'd use parser to parse HTML like HtmlAgilityPack, and use regular expressions to find the color value in attributes.

First, find all the nodes that contain style attribute with color defined in it by using xpath:

var doc = new HtmlDocument();
var nodes = doc.DocumentNode
    .SelectNodes("//*[contains(@style, 'color')]")

Then the simplest regex to match a color value: (?<=color:\s*)#?\w+.

var colorRegex = new Regex(@"(?<=color:\s*)#?\w+", RegexOptions.IgnoreCase);

Then iterate through these nodes and if there is a regex match, replace the inner html of the node with html encoded tags (you'll understand why a little bit later):

foreach (var node in nodes)
    var style = node.Attributes["style"].Value;
    if (colorRegex.IsMatch(style))
        var color = colorRegex.Match(style).Value;
        node.InnerHtml =
            HttpUtility.HtmlEncode("<" + color + ">") +
            node.InnerHtml +
            HttpUtility.HtmlEncode("</" + color + ">");

And finally get the inner text of the document and perform html decoding on it (this is because inner text strips all the tags):

var txt = HttpUtility.HtmlDecode(doc.DocumentNode.InnerText);

This should return something like this:

This is a sample html text.
<#ff9999>this is only a sample</#ff9999>
and some other tags...

Of course you could improve it for your needs.

share|improve this answer
Very thanks. this is what I want.it works very good. –  Ali.M Apr 19 '12 at 5:35
add comment

It is possible to do it using regular expressions but... You should not parse (X)HTML with regex.

The first regexp I came with to solve the problem is:


Group 5th will be the hex (3 or 6 hexadecimals) colour and group 6th will be the text inside the tag.

Obviously, it's not the best solution as I'm not a regexp master and obviously it needs some testing and probably generalisation... But still it's a good point to start with.

share|improve this answer
thanks, but it won't work. I am working more with regex to find out a solutin with it. –  Ali.M Apr 18 '12 at 10:05
add comment

Your Answer


