ubb转换html,UBB代码转换成HTML代码

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

UBB代码转换成HTML代码

[img]http://www.baidu.com[/img]

[link=http://www.baidu.com]百度[/link]

[font=#FF0000]红色[/font]

有这三种格式的UBB代码,先在我要做三个函数分别转换成HTML代码

转换后的结果如下

百度

红色

请问该怎么写这三个函数?

--------------------编程问答--------------------

/// 

/// 替换HTML中的UBB链接代码

/// 

/// 

/// 

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, "" + mh.Groups[2].Value + "");

}

return str;

}

/// 

/// 替换HTML中的图片UBB代码

/// 

/// 

/// 

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, "");

}

return str;

}

/// 

/// 替换HTML中的文字高亮UBB代码

/// 

/// 

/// 

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, "" + mh.Groups[2].Value + "");

}

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]两个

要怎么改?

/// 

/// 替换HTML中的UBB链接代码

/// 

/// 

/// 

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, "" + mh.Groups[2].Value + "");

}

return str;

}

这是我匹配link标记的代码

补充:.NET技术 , C#

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值