C#中验证文章的正则表达式可以这样写

以前只是在ASP中写留言本的时候用到过正则表达式,都是仿照网络上的代码,但是现在是用C#写.net代码了,再用ASP的代码已经不实际了,找了好多同学好多老师都没有解决,没有办法只有自己写了,其实我不愿意自己写的原因是对C#中调用正则表达式的类不熟悉,关在家里一天,终于还是写出来了,而且也考虑到了很多实际情况,修改了以前在ASP中的验证功能,比如:当拉取网络中的图片的时候要是图片很大,网页就会被撑的很大,也就是宽度大于1024px了,所以加了一些JAVASCRIPT代码。

下面是代码,很有用的对那些学C#的人来说。

public string changechr(string chr)

{

if(chr==null)

return "";                 //用RegExp类正则表达式来匹配

chr=chr.Replace("<","&lt");    //Replace: 用替换字符串替换匹配的表达式

chr=chr.Replace(">","&gt");

chr=chr.Replace(" ","&nbsp");

chr=chr.Replace("\n","<br>");

chr = Regex.Replace(chr,@"<script(?<x>[^\>]*)>(?<y>[^\>]*)\</script\>",@"&lt;script$1&gt;$2&lt;/script&gt;",RegexOptions.IgnoreCase);

chr = Regex.Replace(chr,@"\[url=(?<x>[^\]]*)\](?<y>[^\]]*)\[/url\]",@"<a href=$1 target=_blank>$2</a>",RegexOptions.IgnoreCase);

chr = Regex.Replace(chr,@"\[url\](?<x>[^\]]*)\[/url\]",@"<a href=$1 target=_blank>$1</a>",RegexOptions.IgnoreCase);

chr = Regex.Replace(chr,@"\[Email=(?<x>[^\]]*)\](?<y>[^\]]*)\[/Email\]",@"<a href=""mailto:$1"">$2</a>",RegexOptions.IgnoreCase);

chr = Regex.Replace(chr,@"\[Email\](?<x>[^\]]*)\[/Email\]",@"<a href=""mailto:$1"">$1</a>",RegexOptions.IgnoreCase);

chr = Regex.Replace(chr,@"\[flash](?<x>[^\]]*)\[/flash]",@"<OBJECT codeBase=http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=4,0,2,0 classid=clsid:D27CDB6E-AE6D-11cf-96B8-444553540000 width=500 height=400><PARAM NAME=movie VALUE=""$1""><PARAM NAME=quality VALUE=high><embed src=""$1"" quality=high pluginspage='http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash' type='application/x-shockwave-flash' width=500 height=400>$1</embed></OBJECT>",RegexOptions.IgnoreCase);

chr = Regex.Replace(chr,@"\",@"<IMG SRC=""$1"" border=0>",RegexOptions.IgnoreCase);

chr = Regex.Replace(chr,@"\[color=(?<x>[^\]]*)\](?<y>[^\]]*)\[/color\]",@"<font color=$1>$2</font>",RegexOptions.IgnoreCase);

chr = Regex.Replace(ch*,@"\[**ce=(?<x>[^\]]*)\](?<y>[^\]]*)\[/face\]",@"<font face=$1>$2</font>",RegexOptions.IgnoreCase);

chr = Regex.Replace(chr,@"\[size=1\](?<x>[^\]]*)\[/size\]",@"<font size=1>$1</font>",RegexOptions.IgnoreCase);

chr = Regex.Replace(chr,@"\[size=2\](?<x>[^\]]*)\[/size\]",@"<font size=2>$1</font>",RegexOptions.IgnoreCase);

chr = Regex.Replace(chr,@"\[size=3\](?<x>[^\]]*)\[/size\]",@"<font size=3>$1</font>",RegexOptions.IgnoreCase);

chr = Regex.Replace(chr,@"\[size=4\](?<x>[^\]]*)\[/size\]",@"<font size=4>$1</font>",RegexOptions.IgnoreCase);

chr = Regex.Replace(chr,@"\[align=(?<x>[^\]]*)\](?<y>[^\]]*)\[/align\]",@"<align=$1>$2</align>",RegexOptions.IgnoreCase);

chr = Regex.Replace(chr,@"\[fly](?<x>[^\]]*)\[/fly]",@"<marquee width=90% behavior=alternate scrollamount=3>$1</marquee>",RegexOptions.IgnoreCase);

chr = Regex.Replace(chr,@"\[move](?<x>[^\]]*)\[/move]",@"<marquee scrollamount=3>$1</marquee>",RegexOptions.IgnoreCase);

chr = Regex.Replace(chr,@"\[glow=(?<x>[^\]]*),(?<y>[^\]]*),(?<z>[^\]]*)\](?<w>[^\]]*)\[/glow\]",@"<table width=$1 style='filter:glow(color=$2, strength=$3)'>$4</table>",RegexOptions.IgnoreCase);

chr = Regex.Replace(chr,@"\[shadow=(?<x>[^\]]*),(?<y>[^\]]*),(?<z>[^\]]*)\](?<w>[^\]]*)\[/shadow\]",@"<table width=$1 style='filter:shadow(color=$2, strength=$3)'>$4</table>",RegexOptions.IgnoreCase);

chr = Regex.Replace(chr,@"\[b\](?<x>[^\]]*)\[/b\]",@"<b>$1</b>",RegexOptions.IgnoreCase);

chr = Regex.Replace(chr,@"\[i\](?<x>[^\]]*)\[/i\]",@"<i>$1</i>",RegexOptions.IgnoreCase);

chr = Regex.Replace(chr,@"\[u\](?<x>[^\]]*)\[/u\]",@"<u>$1</u>",RegexOptions.IgnoreCase);

chr = Regex.Replace(chr,@"\[code\](?<x>[^\]]*)\[/code\]",@"<pre id=code><font size=1 face='Verdana, Arial' id=code>$1</font id=code></pre id=code>",RegexOptions.IgnoreCase);

chr = Regex.Replace(chr,@"\[list\](?<x>[^\]]*)\[/list\]",@"<ul>$1</ul>",RegexOptions.IgnoreCase);

chr = Regex.Replace(chr,@"\[list=1\](?<x>[^\]]*)\[/list\]",@"<ol type=1>$1</ol id=1>",RegexOptions.IgnoreCase);

chr = Regex.Replace(chr,@"\[list=a\](?<x>[^\]]*)\[/list\]",@"<ol type=a>$1</ol id=a>",RegexOptions.IgnoreCase);

chr = Regex.Replace(chr,@"\[\*\](?<x>[^\]]*)\[/\*\]",@"<li>$1</li>",RegexOptions.IgnoreCase);


return(chr);

}

原文地址:https://www.cnblogs.com/ymyglhb/p/1263464.html