正则表达式BBCode到HTML [英] Regex BBCode to HTML

查看:201
本文介绍了正则表达式BBCode到HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将BBcode转换器编写为html.
转换器应跳过未关闭的标签.

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

我考虑过两种选择:
1)使用一个正则表达式调用一次匹配所有标签,例如:

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

Regex re2 = new Regex(@"\[(\ /?(?:b|i|u|quote|strike))\]");
MatchCollection mc = re2.Matches(sourcestring);

然后,使用2个指针在MatchCollection上循环查找开始和打开标记,然后用正确的html标记替换.

and then, loop over MatchCollection using 2 pointers to find start and open tags and than replacing with right html tag.

2)为每个标签多次调用正则表达式并直接替换:

2) call regex multiple time for every tag and replace directly:

Regex re = new Regex(@"\[b\](.*?)\[\/b\]"); 
string s1 = re.Replace(sourcestring2,"<b>$1</b>");

什么是更有效的?

第一个选项使用一个正则表达式,但将要求我遍历所有标签并查找所有对,并跳过没有对的标签.
另一个好处是,我不在乎标签之间的内容,我只是工作并使用位置替换它们.

The first option uses one regex but will require me to loop through all tags and find all pairs, and skip tags that don't have a pair.
Another positive thins is that I don't care about the content between the tags, i just work and replace them using the position.

在第二个选项中,我不必担心循环和进行特殊的替换功能.
但是将需要执行多个正则表达式并替换.

In second option I don't need to worry about looping and making special replace function.
But will require to execute multiple regex and replaces.

您有什么建议?

如果第二个选项是正确的, 正则表达式有问题 \[b\](.*?)\[\/b\]

If the second option is the right one, there is a problem with regex \[b\](.*?)\[\/b\]

我如何解决它以匹配多行,如:

how can i fix it to also match multi lines like:

[b]
        test 1
[/b]

[b]
        test 2
[/b]

推荐答案

一种选择是使用更多类似于SAX的解析,而不是查找特定的正则表达式,而是查找[,然后由程序处理即使以某种方式,也要查找],甚至还要处理它.等等.尽管比正则表达式更冗长,但可能更易于理解,并且不一定会更慢.

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.

这篇关于正则表达式BBCode到HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆