Regex BBCode to HTML

前端 未结 2 1053
误落风尘
误落风尘 2021-01-22 00:17

I writing BBcode converter to html.
Converter should skip unclosed tags.

I thought about 2 options to do it:
1) match all tags in once using one regex call, like

相关标签:
2条回答
  • 2021-01-22 00:49

    One option would be to use more SAX-like parsing, where instead of looking for a particular regex you look for [, then have your program handle that even in some manner, look for the ], handle that even, etc. Although more verbose than the regex it may be easier to understand, and wouldn't necessarily be slower.

    0 讨论(0)
  • 2021-01-22 01:01
    r = new System.Text.RegularExpressions.Regex(@"(?:\[b\])(?<name>(?>\[b\](?<DEPTH>)|\[/b\](?<-DEPTH>)|.)+)(?(DEPTH)(?!))(?:\[/b\])", System.Text.RegularExpressions.RegexOptions.Singleline);
    
     var s = r.Replace("asdfasdf[b]test[/b]asdfsadf", "<b>$1</b>");
    

    That should give you only elements that have matched closing tags and also handle multi line (even though i specified the option of SingleLine it actually treats it as a single line)

    It should also handle [b][b][/b] properly by ignoring the first [b].

    As to whether or not this method is better than your first method I couldn't say. But hopefully this will point you in the right direction.

    Code that works with your example below: System.Text.RegularExpressions.Regex r;

    r = new System.Text.RegularExpressions.Regex(@"(?:\[b\])(?<name>(?>\[b\](?<DEPTH>)|\[/b\](?<-DEPTH>)|.)+)(?(DEPTH)(?!))(?:\[/b\])", System.Text.RegularExpressions.RegexOptions.Singleline);
    
    var s = r.Replace("[b]bla bla[/b]bla bla[b] " + "\r\n" + "bla bla [/b]", "<b>$1</b>");
    
    0 讨论(0)
提交回复
热议问题