#region Regex
public static string FilterScript(string content)
{
if(content==null || content==”")
{
return content;
}
string regexstr=@”(?i)<script([^>])*>(\w|\W)*</script([^>])*>”;//@”<script.*</script>”;
content=Regex.Replace(content,regexstr,string.Empty,RegexOptions.IgnoreCase);
content=Regex.Replace(content,”<script([^>])*>”,string.Empty,RegexOptions.IgnoreCase);
return Regex.Replace(content,”</script>”,string.Empty,RegexOptions.IgnoreCase);
}
public static string FilterIFrame(string content)
{
if(content==null || content==”")
{
return content;
}
string regexstr=@”(?i)<iframe([^>])*>(\w|\W)*</iframe([^>])*>”;//@”<script.*</script>”;
content=Regex.Replace(content,regexstr,string.Empty,RegexOptions.IgnoreCase);
content=Regex.Replace(content,”<iframe([^>])*>”,string.Empty,RegexOptions.IgnoreCase);
return Regex.Replace(content,”</iframe>”,string.Empty,RegexOptions.IgnoreCase);
}
public static string CheckHtml(string content)
{
string regexfont1=@”(?i)<font([^>])*>(\w|\W)*</font([^>])*>”;//@”<script.*</script>”;
string regexfont2=@”<font([^>])*>”;
if(Regex.IsMatch(content,regexfont2)&&(!Regex.IsMatch(content,regexfont1)))
{
return Regex.Replace(content,regexfont2,string.Empty,RegexOptions.IgnoreCase);
}
return content;
}
public static string RemoveHtml(string content)
{
string newstr=FilterScript(content);
string regexstr=@”<[^>]*>”;
return Regex.Replace(newstr,regexstr,string.Empty,RegexOptions.IgnoreCase);
}
public static string Removenbsp(string content)
{
string newstr=FilterScript(content);
string regexstr=@” ”;
return Regex.Replace(newstr,regexstr,string.Empty,RegexOptions.IgnoreCase);
}
public static string Removefullspace(string content)
{
string newstr=FilterScript(content);
string regexstr=@” ”;
return Regex.Replace(newstr,regexstr,string.Empty,RegexOptions.IgnoreCase);
}
public static string RemoveHtmlTag(string content,string[] tags)
{
string regexstr1,regexstr2;
foreach(string tag in tags)
{
if(tag!=”")
{
regexstr1=string.Format(@”<{0}([^>])*>”,tag);
regexstr2=string.Format(@”</{0}([^>])*>”,tag);
content=Regex.Replace(content,regexstr1,string.Empty,RegexOptions.IgnoreCase);
content=Regex.Replace(content,regexstr2,string.Empty,RegexOptions.IgnoreCase);
}
}
return content;
}
public static string RemoveHtmlTag(string content,string tag)
{
string returnStr;
string regexstr1=string.Format(@”<{0}([^>])*>”,tag);
string regexstr2=string.Format(@”</{0}([^>])*>”,tag);
returnStr=Regex.Replace(content,regexstr1,string.Empty,RegexOptions.IgnoreCase);
returnStr=Regex.Replace(returnStr,regexstr2,string.Empty,RegexOptions.IgnoreCase);
return returnStr;
}
public static string ReplaceSpace(string content)
{
string findstr=”(?<fore>(?:(?:[^< ])*(?:<(?:!–(?:(?:[^-])*(?:(?=–>)|-))*–|(?:[^>])+)>)?)*)[ ](?<back>(?:(?:[^< ])*(?:<(?:!–(?:(?:[^-])*(?:(?=–>)|-))*–|(?:[^>])+)>)?)*)”;
//”(?<fore>(?:[^< ]*(?:<[^>]+>)?)*)[ ](?<back>(?:[^< ]*(?:<[^>]+>)?)*)”;
string replacestr=”${fore} ${back}”;
string targetstr=System.Text.RegularExpressions.Regex.Replace(content,findstr,replacestr,System.Text.RegularExpressions.RegexOptions.IgnoreCase);
return targetstr;
}
public static string[] CatchHtmlBlock(string content,string tag)
{
string findstr=string.Format(@”(?i)<{0}([^>])*>(\w|\W)*</{1}([^>])*>”,tag,tag);
System.Text.RegularExpressions.MatchCollection matchs=System.Text.RegularExpressions.Regex.Matches(content,findstr,System.Text.RegularExpressions.RegexOptions.IgnoreCase);
string[] strArray=new string[matchs.Count];
for(int i=0;i<strArray.Length;i++)
{
strArray[i]=matchs[i].Value;
}
return strArray;
}
public static bool IsNumeric(string text)
{
return Regex.IsMatch(text,”^\\d+$”);
}
留言列表