C# 如何找到特定BBCode标记(Regex)的索引位置

C# 如何找到特定BBCode标记(Regex)的索引位置,c#,regex,bbcode,C#,Regex,Bbcode,我有一根绳子,比如说: [s]AB[/s]23[sb]45[/sb]AB45ABABAB 我想找到由包含字母s的标记包围的所有索引,因此包括[s]和[sb] 对findinges(“[s]01[/s]23[sb]45[/sb]ab45abababab”,“s”)的函数调用将返回列表[0,1,4,5]。请注意,索引忽略所有BBCode。换句话说,它认为第一个“A”字符的索引是0,而不是3 如何在C#中实现findindice。我尝试使用System.Text.RegularExpressions

我有一根绳子,比如说:

[s]AB[/s]23[sb]45[/sb]AB45ABABAB
我想找到由包含字母s的标记包围的所有索引,因此包括[s]和[sb]

对findinges(“[s]01[/s]23[sb]45[/sb]ab45abababab”,“s”)的函数调用将返回列表[0,1,4,5]。请注意,索引忽略所有BBCode。换句话说,它认为第一个“A”字符的索引是0,而不是3


如何在C#中实现findindice。我尝试使用System.Text.RegularExpressions,但我遇到了问题,困难在于找到与去掉BBCode的字符串相关的索引。

类似的正则表达式模式
(?这只是一个示例,您可以这样尝试,在此处测试:

公共类实体
{
公共字符串文本{get;set;}
公共int索引{get;set;}
公共int CountDirty{get;set;}
public int CountClean{get;set;}
公共int CountGross{get;set;}
public int IndexStart{get;set;}
public int IndexEnd{get;set;}
public int IndexStartClean{get;set;}
public int IndexEndClean{get;set;}
public int IndexStartGross{get;set;}
public int IndexEndGross{get;set;}
公共int CountBefore{get;set;}
公共int CountAfter{get;set;}
}
公共静态列表findIndices(字符串文本)
{
字符串regex=@“(\[[a-zA-Z]*\])(.*)(\[/[[a-zA-Z]*\])”;
正则表达式r=新正则表达式(正则表达式);
MatchCollection matches=r.matches(文本);
列表=新列表();
整数累加=0;
foreach(匹配中的匹配)
{
实体t=新实体();
字符串stringa2=match.ToString();
t、 CountBefore=match.Groups[1].ToString().Count();
t、 CountAfter=match.Groups[3].ToString().Count();
t、 CountClean=match.Groups[2].ToString().Count();
t、 CountGross=match.ToString().Count();
t、 CountDirty=t.CountClean-t.CountGross;
t、 Text=stringa2;
t、 IndexStart=match.Index;
t、 Index=match.Index+t.CountGross-1;
t、 IndexStartGross=t.IndexStart+t.CountBefore;
t、 IndexEndGross=t.IndexStartGross+t.CountClean-1;
t、 IndexStartClean=t.IndexStartGross-t.CountBefore-累加;
t、 IndexEndClean=t.IndexStartClean+t.CountClean-1;
列表。添加(t);
累加+=t.CountBefore+t.CountAfter;
}
退货清单;
}
这是一个实现:

        List<Entity> list = findIndices("[s]AB[/s]23[sb]45[/sb]AB45ABABAB[a]test[/a]");

        for (var i = 0; i < list.Count; i++)
        {
            var l = list[i];

            Console.WriteLine("Text = " + l.Text);

            Console.WriteLine("IndexStartClean = " + l.IndexStartClean);
            Console.WriteLine("IndexEndClean = " + l.IndexEndClean);

            Console.WriteLine("---");
        }
List List=findIndices(“[s]AB[/s]23[sb]45[/sb]ab45abababab[a]测试[/a]”);
对于(var i=0;i
您不能只使用正则表达式。请参阅我的答案。@Laurianti根据它应该如何处理嵌套不好的标记,正则表达式实际上可能是实现此目的的理想工具。您需要一个图例。谢谢!
        List<Entity> list = findIndices("[s]AB[/s]23[sb]45[/sb]AB45ABABAB[a]test[/a]");

        for (var i = 0; i < list.Count; i++)
        {
            var l = list[i];

            Console.WriteLine("Text = " + l.Text);

            Console.WriteLine("IndexStartClean = " + l.IndexStartClean);
            Console.WriteLine("IndexEndClean = " + l.IndexEndClean);

            Console.WriteLine("---");
        }