C# 过滤字符串

Specialaspnet 2010-10-29 03:50:46
c#过滤特定的字符之间的所有字符串

我想过滤这个[img]---[/img],这个标签和之间左右的字符串,应该怎么过滤?

...全文
409 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
Specialaspnet 2010-10-30
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 wuyazhe 的回复:]

C# code
string[] items = Regex.Split(html,@"(?is)\[img\].+?\[/img\]");

items就是你要的结果。
下午出去了。才看到你消息。
[/Quote]

额,谢谢啊,已经解决了
兔子-顾问 2010-10-29
  • 打赏
  • 举报
回复
string[] items = Regex.Split(html,@"(?is)\[img\].+?\[/img\]");

items就是你要的结果。
下午出去了。才看到你消息。
xiatiandetonghua 2010-10-29
  • 打赏
  • 举报
回复
学习了…
cc1122112 2010-10-29
  • 打赏
  • 举报
回复
用正则
Specialaspnet 2010-10-29
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 tingting74998 的回复:]

text = Regex.Replace(text, @"(?<=[img])[\s\S]*?(?=[/img])", "这里是替换的内容,可以为空", RegexOptions.IgnoreCase);

把[img] 和[/img]中间的替换成空就OK了
[/Quote]

但是我要把标签也除掉啊,也就是[img][/img]
Specialaspnet 2010-10-29
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 kkbac 的回复:]

C# code

using System.Text.RegularExpressions;

Regex regex = new Regex(
@"(?<=\[img\])([\s\S]*?)(?=\[/img\])",
RegexOptions.IgnoreCase
);
[/Quote]

这个是[img][/img]没有除掉
kkbac 2010-10-29
  • 打赏
  • 举报
回复

using System.Text.RegularExpressions;

Regex regex = new Regex(
@"(?<=\[img\])([\s\S]*?)(?=\[/img\])",
RegexOptions.IgnoreCase
);
tingting74998 2010-10-29
  • 打赏
  • 举报
回复
text = Regex.Replace(text, @"(?<=[img])[\s\S]*?(?=[/img])", "这里是替换的内容,可以为空", RegexOptions.IgnoreCase);

把[img] 和[/img]中间的替换成空就OK了
Specialaspnet 2010-10-29
  • 打赏
  • 举报
回复
大家快点过来帮帮忙啊 呜呜呜呜呜呜uwuuu
Specialaspnet 2010-10-29
  • 打赏
  • 举报
回复
大家快点过来帮帮忙啊
Specialaspnet 2010-10-29
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 cys1016116747 的回复:]

public static string StripHTML(string strHtml)
{
//HTML标签数组
string[] aryReg ={
@"<script[^>]*?>.*?</script>",
@"<(\/\s*)?!?((\w+:)?\w+)(\w+(\s*=?\s*((……
[/Quote]

我的问题不是过滤html额
cys1016116747 2010-10-29
  • 打赏
  • 举报
回复
public static string StripHTML(string strHtml)
{
//HTML标签数组
string[] aryReg ={
@"<script[^>]*?>.*?</script>",
@"<(\/\s*)?!?((\w+:)?\w+)(\w+(\s*=?\s*(([""'])(\\[""'tbnr]|[^\7])*?\7|\w+)|.{0})|\s)*?(\/\s*)?>",
@"([\r\n])[\s]+",
@"&(quot|#34);",
@"&(amp|#38);",
@"&(lt|#60);",
@"&(gt|#62);",
@"&(nbsp|#160);",
@"&(iexcl|#161);",
@"&(cent|#162);",
@"&(pound|#163);",
@"&(copy|#169);",
@"&#(\d+);",
@"-->",
@"<!--.*\n"
};
string[] aryRep = {
"",
"",
"",
"\"",
"&",
"<",
">",
" ",
"\xa1",//chr(161),
"\xa2",//chr(162),
"\xa3",//chr(163),
"\xa9",//chr(169),
"",
"\r\n",
""
};
string newReg = aryReg[0];
string strOutput = strHtml;
for (int i = 0; i < aryReg.Length; i++)
{
Regex regex = new Regex(aryReg[i], RegexOptions.IgnoreCase);
strOutput = regex.Replace(strOutput, aryRep[i]);
}
strOutput.Replace("<", "");
strOutput.Replace(">", "");
strOutput.Replace("\r\n", "");
strOutput.Replace(" ", " ");
return strOutput;
}
Specialaspnet 2010-10-29
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 shaoliang520xi 的回复:]

//主要内容,包含标签本身
public static Regex regMainContent = new Regex(@"[img][\s\S]*?[/img]", RegexOptions.IgnoreCase);
[/Quote]

public static string LostHTML(string Str)
{
string Re_Str = "";
if (Str != null)
{
if (Str != string.Empty)
{
string Pattern = @"[img][\s\S]*?[/img]";
Re_Str = Regex.Replace(Str, Pattern,"");
}
}
return (Re_Str.Replace(" \\r\\n", "")).Replace("\\r", "").Replace("\r", "").Replace("\n", "");
}


请问这样写 可以不
Specialaspnet 2010-10-29
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 shaoliang520xi 的回复:]

//主要内容,包含标签本身
public static Regex regMainContent = new Regex(@"[img][\s\S]*?[/img]", RegexOptions.IgnoreCase);
[/Quote]

details = ds.Tables[0].Rows[i][0].ToString();我就是要把这个details里面的过滤掉,那怎么整合在一起呢,我试了一下好像不可以额
shaoliang520xi 2010-10-29
  • 打赏
  • 举报
回复
//主要内容,包含标签本身
public static Regex regMainContent = new Regex(@"[img][\s\S]*?[/img]", RegexOptions.IgnoreCase);

62,272

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧