Search code examples
.netstringbbcode

How to remove BBCode from a string in .Net


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

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;