当前位置:编程学习 > C#/ASP.NET >>

UBB代码转换成HTML代码

[img]http://www.baidu.com[/img]
[link=http://www.baidu.com]百度[/link]
[font=#FF0000]红色[/font]
有这三种格式的UBB代码,先在我要做三个函数分别转换成HTML代码
转换后的结果如下
<img src="http://www.baidu.com" />
<a href="http://www.baidu.com">百度</a>
<span style="color:#FF0000">红色</span>

请问该怎么写这三个函数?
--------------------编程问答-------------------- /// <summary>
        /// 替换HTML中的UBB链接代码
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string ReplaceUBBLink(string str)
        {
            Regex re = new Regex(@"\[link=(.{7,})\](.{1,})\[/link\]", RegexOptions.IgnoreCase);
            MatchCollection matches = re.Matches(str);
            foreach (Match mh in matches)
            {
                str = str.Replace(mh.Groups[0].Value, "<a href=\"" + mh.Groups[1].Value + "\">" + mh.Groups[2].Value + "</a>");
            }
            return str;
        }
        /// <summary>
        /// 替换HTML中的图片UBB代码
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string ReplaceUBBImg(string str)
        {
            Regex re = new Regex(@"\[img\](.{7,})\[/img\]", RegexOptions.IgnoreCase);
            MatchCollection matches = re.Matches(str);
            foreach (Match mh in matches)
            {
                str = str.Replace(mh.Groups[0].Value, "<img src=\"" + mh.Groups[1].Value + "\" />");
            }
            return str;
        }
        /// <summary>
        /// 替换HTML中的文字高亮UBB代码
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string ReplaceUBBColor(string str)
        {
            Regex re = new Regex(@"\[font=(.{7,})\](.{1,})\[/font\]", RegexOptions.IgnoreCase);
            MatchCollection matches = re.Matches(str);
            foreach (Match mh in matches)
            {
                str = str.Replace(mh.Groups[0].Value, "<span style=\"color:" + mh.Groups[1].Value + ";\">" + mh.Groups[2].Value + "</span>");
            }
            return str;
        }

自己解决了! --------------------编程问答-------------------- 你用正则匹配 是个好方法。 --------------------编程问答-------------------- 又出问题了!~查了下百度,我这个是贪婪模式匹配的
[link=http://www.baidu.com]发货[/link][link=http://www.baidu.com]百度一下,你就知道[/link]
会作为一个[link]匹配到
我要的是[link=http://www.baidu.com]发货[/link]和[link=http://www.baidu.com]百度一下,你就知道[/link]两个

要怎么改?
/// <summary>
        /// 替换HTML中的UBB链接代码
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string ReplaceUBBLink(string str)
        {
            Regex re = new Regex(@"\[link=(.{7,})\](.*?)\[/link\]", RegexOptions.IgnoreCase);
            MatchCollection matches = re.Matches(str);
            foreach (Match mh in matches)
            {
                str = str.Replace(mh.Groups[0].Value, "<a href=\"" + mh.Groups[1].Value + "\">" + mh.Groups[2].Value + "</a>");
            }
            return str;
        }
这是我匹配link标记的代码
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,