当前位置:编程学习 > 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#