正则表达式,替换所有HTML标签。

我自己写了一个正则表达式,<(.|\n)+?>
这个是替换所以HTML标签,非贪婪的,多行的。

 

 如果我想替换得到所以非HTML标签,
             我的代码就只能是这样,先找打HTML标签,然后将标签替换掉。

 

 能不能直接找到非HTML标签呢。。

 

还有个问题就是,,截取字符串的长度。 
         我下面的这种方法,没有判断中文或者非中文,截取的长度总是有长有短。 
             不知道有没有好点的办法让截取的长度,一样长的,而不是str.Length的长度。 
Code
public static string formatString(string str, int size)
{
string temp = str;
//Encoding.Default.GetBytes(str).Length;
Regex regex = new Regex("<.+?>");
temp
= regex.Replace(str, "");
//for (int i = 0; i < regex.Matches(str).Count; i++)
//{
// temp = temp.Replace(regex.Matches(str)[i].Value, "");
//}
temp = temp.Replace("\r\n", "");
temp
= temp.Replace("&nbsp;", "");
if (temp.Length >= size)
{
temp
= temp.Substring(0, size - 3) + "";
}
return temp;
}

 

转载于:https://www.cnblogs.com/fycayy/archive/2008/11/05/regex.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值