我發現/製作了一個正則表達式,它可以很好地解析bbtags,但它有一個缺陷,它不會處理與包含標記相同的嵌套標記。嵌套BBTags的正則表達式
的BBTag解析器我做依賴於一個事實,即最外層標籤可以包含正常工作的子標籤,如果嵌套的標籤是不一樣的含標籤
我使用的正則表達式是:@"\[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.");