C# 用于格式化嵌套BB[quote]标记的正则表达式

C# 用于格式化嵌套BB[quote]标记的正则表达式,c#,regex,replace,C#,Regex,Replace,我正试图转向: [quote]Hello![/quote] 进入: 结果是: [quote] test [quote] nest <div class="quoted-text"> nest </div> [/quote] [/quote] [引用] 测试 [引述] 巢 巢 [quote] [quote] 有人能告诉我如何按预期完成这项工作吗?我看不出您需要将报价解析为块

我正试图转向:

[quote]Hello![/quote]
进入:

结果是:

[quote]
    test
    [quote]
        nest
        <div class="quoted-text">
            nest
        </div>
    [/quote]
[/quote]
[引用]
测试
[引述]
巢
巢
[quote]
[quote]

有人能告诉我如何按预期完成这项工作吗?

我看不出您需要将报价解析为块的原因。因此,最简单的解决方案是用您想要的令牌替换每个令牌。以下是LinqPad的一个示例:

void Main()
{

    var rawComment =
"[quote]\n" +
"   test\n" +
"   [quote]\n" +
"       nest\n" +
"       [quote]\n" +
"           nest\n" +
"       [/quote]\n" +
"   [/quote]\n" +
"[/quote]\n";

    var start = new Regex(@"\[quote\]", RegexOptions.IgnoreCase);
    var end = new Regex(@"\[\/quote\]", RegexOptions.IgnoreCase);
    rawComment = start.Replace(rawComment, "<div class=\"quoted-text\">");
    rawComment = end.Replace(rawComment, "</div>");

    rawComment.Dump();
}
void Main()
{
var rawComment=
“[引号]\n”+
“测试\n”+
“[引号]\n”+
“嵌套\n”+
“[引号]\n”+
“嵌套\n”+
“[/quote]\n”+
“[/quote]\n”+
“[/quote]\n”;
var start=newregex(@“\[quote\]”,RegexOptions.IgnoreCase);
var end=newregex(@“\[\/quote\]”,RegexOptions.IgnoreCase);
rawcoment=start.Replace(rawcoment,“”);
rawcoment=end.Replace(rawcoment,“”);
rawcoment.Dump();
}
这将产生以下结果:

<div class="quoted-text">
  test
  <div class="quoted-text">
    nest
    <div class="quoted-text">
      nest
    </div>
  </div>
</div>

测试
巢
巢

我也遇到了同样的问题,通过编写解析器解决了这个问题。使用正则表达式管理所有转换太困难了。希望您能找到更简单的解决方案!:)谢谢,但是格式错误的评论(例如,[quotes]多于[[quotes])会破坏整个系统page@TomGullen:对于这种结构不正确的情况,没有其他解决办法。为什么不使用<代码>堆栈< /代码>呢?我认为标记的验证是标记转换的独立关注点。
[quote]
    test
    [quote]
        nest
        <div class="quoted-text">
            nest
        </div>
    [/quote]
[/quote]
void Main()
{

    var rawComment =
"[quote]\n" +
"   test\n" +
"   [quote]\n" +
"       nest\n" +
"       [quote]\n" +
"           nest\n" +
"       [/quote]\n" +
"   [/quote]\n" +
"[/quote]\n";

    var start = new Regex(@"\[quote\]", RegexOptions.IgnoreCase);
    var end = new Regex(@"\[\/quote\]", RegexOptions.IgnoreCase);
    rawComment = start.Replace(rawComment, "<div class=\"quoted-text\">");
    rawComment = end.Replace(rawComment, "</div>");

    rawComment.Dump();
}
<div class="quoted-text">
  test
  <div class="quoted-text">
    nest
    <div class="quoted-text">
      nest
    </div>
  </div>
</div>