您的位置:程序门 -> .net技术 -> j#



一个简单的正则替换 问题 帮帮忙


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


一个简单的正则替换 问题 帮帮忙[已结贴,结贴人:aierduo]
发表于:2007-12-29 15:14:47 楼主
我的字符串是这样
s=" <td   valign=\"top\"   style=\"font-size:   10.5pt;\"> <strong> smart   &   biggar";

想将它截取为   smart   &   biggar
现在用正则表达式替换 <> 之间的字符串,如下:
regex.replace(s,   " <.+> ",   "");

但是实际它没替换,请问应该怎么写。
谢谢
发表于:2007-12-29 15:24:561楼 得分:5
替换html字符串的正则:
\ <[^\ <,\> ]+\>
发表于:2007-12-29 15:28:522楼 得分:5
c# code
string str="<td valign=\"top\" style=\"font-size: 10.5pt;\"> <strong> smart & biggar"; string a = system.text.regularexpressions.regex.replace(str,@"<td valign=""top"" style=""font-size: 10.5pt;""> <strong>(.*?)","$1"); response.write(a);
发表于:2007-12-29 15:30:003楼 得分:10

c# code
string s = " <td valign=\"top\" style=\"font-size: 10.5pt;\"> <strong> smart & biggar"; string result = regex.replace(s, "<[^>]*>", "");
发表于:2007-12-29 15:32:014楼 得分:0
sbqcel  
具体怎么写我不清楚啊,能帮我写一下吗,谢谢

wangkun9999    
如果这样得话我直接用字符串得replace就行了。
何必这么替换。
现在主要是我不知道 <> 里得内容是甚么

发表于:2007-12-29 15:36:145楼 得分:0
谢谢大家  
发表于:2007-12-29 15:38:526楼 得分:0
调用下面的方法:  
c# code
/// <summary> /// html代码替换 /// </summary> /// <param name="strcontent"></param> /// <returns></returns> public static string replacehtmlcode(string strcontent) { string text1 = @"\<[^\<,\>]+\>"; regex regex1 = new regex(text1, regexoptions.singleline | regexoptions.compiled | regexoptions.ignorecase); matchcollection collection1 = regex1.matches(strcontent); forint num1 = 0; num1 < collection1.count; num1++) { strcontent = strcontent.replace(collection1[num1].value, ""); } return strcontent; }


快速检索

最新资讯