Reputation: 2282
I have found/made a regex that parses bbtags great but it has one flaw, it doesn't treat nested tag that are are the same as the containing tag.
The BBTag parser I made relies on the fact that the outermost tags can contain sub tags which works fine if nested tag is not the same as the containing tag
The regex I am using is: @"\[color=((.|\n)*?)(?:\s*)\]((.|\n)*?)\[/color(?:\s*)\]"
Test application ( simple console app )
var regex = new Regex(@"\[color=((.|\n)*?)(?:\s*)\]((.|\n)*?)\[/color(?:\s*)\]");
var matches = regex.Matches("[color=red]This is red.[color=green] This be green.[/color] Still red here.[/color] This has no color [color=green] and this is green.[/color]").Cast<Match>().Where(x => x.Success).ToList();
if (matches.Count > 0)
for(int m = 0; m < matches.Count; m++)
for (int i = 0; i < matches[m].Groups.Count; i++)
Console.WriteLine("({0}) - {1} -> {2}", m, i, matches[m].Groups[i]);
else
Console.WriteLine("No matches.");
Upvotes: 1
Views: 86
Reputation: 89567
If you want to match this kind of things (that can be nested) with regex, you can use the balancing group technic:
(?:[^][]|(?<Open>\[([a-z]+)[^]]*])|(?<Content-Open>\[/\1]))+(?(Open)(?!))
You can find interesting informations about this technic here.
Upvotes: 0
Reputation: 171188
Just use a BBCode library. Codekicker.BBCode parses BBCode, converts it to HTML or text and can give you a transformable AST.
Regex is not made for hierarchical parsing. You'll have a hard time making this work. Bugs will be hard to find. Security issues will arise.
Upvotes: 1