Question

I'm trying to remove all BBCode Tags from a string.

[url]www.google.com[/url]

becomes

www.google.com

I have a regex that works in php to find them all, just dont know how to remove them in .net

RegEx to Find BBCode

|[[\/\!]*?[^\[\]]*?]|si
Was it helpful?

Solution

Your regular expression looks like it won't work so I tried a different one:

string s = "[url]www.google.com[/url] [url=www.google.com]www.google.com[/url]";
s = Regex.Replace(s, @"\[[^]]+\]", "");

Result:

www.google.com www.google.com

Also, you will need this using statement at the top of your file to make this work:

using System.Text.RegularExpressions;

OTHER TIPS

I you use Codekicker.BBCode library (this or that) then this code will strip known bbcode tags:

parser.ParseSyntaxTree(@"[url]www.google.com[/url] [url=www.google.com]www.google.com[/url]").ToText()

This will strip only known BB code tags and you need to first create BBCodeParser instance with information about used tags. The default parser used by library is:

var parser = new BBCodeParser(ErrorMode.ErrorFree, null, new[]
{
    new BBTag("b", "<b>", "</b>"),
    new BBTag("i", "<span style=\"font-style:italic;\">", "</span>"),
    new BBTag("u", "<span style=\"text-decoration:underline;\">", "</span>"),
    new BBTag("code", "<pre class=\"prettyprint\">", "</pre>"),
    new BBTag("img", "<img src=\"${content}\" />", "", false, true),
    new BBTag("quote", "<blockquote>", "</blockquote>"),
    new BBTag("list", "<ul>", "</ul>"),
    new BBTag("*", "<li>", "</li>", true, false),
    new BBTag("url", "<a href=\"${href}\">", "</a>", new BBAttribute("href", ""), new BBAttribute("href", "href")),
});

(you need to create it yourself, Codekicker.BBCode doesn't expose this object)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top