涵盖网站基本使用的正则表达式的验证方法.cs
using System;
using System.Text.RegularExpressions;
using System.Text;
using System.IO;
using System.Security.Cryptography;
namespace Chen.Common
{
/// <summary>
/// 数组拓展方法
/// </summary>
public static class StringUtil
{
#region 拓展方法 #region 判断是否为空或者null
/// <summary>
/// 判断是否为空或者null
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static bool IsNullOrEmpty(this string s)
{
return string.IsNullOrEmpty(s);
}
#endregion #region 判断是否为字母
/// <summary>
/// 判断是否为字母
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static bool IsLetter(this string str)
{
return Regex.IsMatch(str, @"^[A-Za-z]+$");
}
#endregion #region 判断是否为数字
/// <summary>
/// 判断是否为数字
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static bool IsNumer(this string str)
{
return Regex.IsMatch(str, @"^\d+$");
}
#endregion #region 判断是否为中文字符
/// <summary>
/// 判断是否为中文字符
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static bool IsChinese(this string s)
{
return Regex.IsMatch(s, @"^[\u4e00-\u9fa5]+$");
}
#endregion #region 判断是否为字母数字组合
/// <summary>
/// 判断是否为字母数字组合
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static bool IsCharNumber(this string s)
{
return Regex.IsMatch(s, @"^\w+$");
}
#endregion #region 判断是否为字母数字下划线组合
/// <summary>
/// 判断是否为字母数字下划线组合
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static bool IsName(this string s)
{
return Regex.IsMatch(s, @"^[a-zA-Z0-9_]+$");
}
#endregion #region 判断是否为字符或是数字
/// <summary>
/// 判断是否为字符或是数字
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static bool IsCharNum(this string s)
{
return Regex.IsMatch(s, @"^[A-Za-z0-9]+$");
}
#endregion #region 判断是整数或小数
/// <summary>
/// 判断是整数或小数
/// </summary>
/// <param name="Expression"></param>
/// <returns></returns>
public static bool IsDouble(this object Expression)
{
int ret;
return int.TryParse(Expression.ToString(), out ret);
}
#endregion #region 判断一个字符串是否是正数型的字符串
/// <summary>
/// 判断一个字符串是否是正数型的字符串
/// </summary>
/// <param name="strValue"></param>
/// <returns></returns>
public static bool IsUnsign(this string strValue)
{
int ret;
if (strValue.Substring(0, 1) == "-")
{
return false;
}
else
{
return int.TryParse(strValue, out ret);
}
}
#endregion #region 判断一个字符串是否是正数型的字符串
/// <判断一个字符串是否是正数型的字符串>
/// 判断一个字符串是否是正数型的字符串
/// </判断一个字符串是否是正数型的字符串>
/// <字符串 name= "strValue "> 字符串 </字符串>
/// <是则返回true,否则返回false> 是则返回true,否则返回false </是则返回true,否则返回false>
public static bool IsUnsignDouble(this string strValue)
{
double ret;
if (strValue.Substring(0, 1) == "-")
{
return false;
}
else
{
return double.TryParse(strValue, out ret);
}
}
#endregion #region 判断是否为Email
/// <summary>
/// 判断是否为Email
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static bool IsEmail(this string s)
{
return Regex.IsMatch(s, @"^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$");
}
#endregion #region 判断是否为IP地址
/// <summary>
/// 判断是否为IP地址
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static bool IsIP(this string s)
{
return Regex.IsMatch(s, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$");
}
#endregion #region 判断是否为URL地址
/// <summary>
/// 判断是否为URL地址
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static bool IsURL(this string s)
{
return Regex.IsMatch(s, @"^([a-zA-z]+://)?(\w+(-\w+)*)(\.(\w+(-\w+)*))*(\?\S*)?$");
}
#endregion #region 判断是否为正确的身份证号码
/// <summary>
/// 判断是否为正确的身份证号码
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static bool IsID(this string s)
{
return Regex.IsMatch(s, @"^(\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$");
}
#endregion #region 判断是否为电话号码格式
/// <summary>
/// 判断是否为电话号码格式
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static bool IsPhone(this string s)
{
return Regex.IsMatch(s, @"((\d{11})|^((\d{7,8})|(\d{4}|\d{3})-(\d{7,8})|(\d{4}|\d{3})-(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1})|(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1}))$)");
}
#endregion #region 判断是否为日期
/// <summary>
/// 判断是否为日期
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static bool IsDate(this string s)
{
try
{
DateTime.Parse(s);
return true;
}
catch
{
return false;
} }
#endregion #region 移除Html标记
/// <summary>
/// 移除Html标记
/// </summary>
/// <param name="content"></param>
/// <returns></returns>
public static string RemoveHtml(this string content)
{
string regexstr = @"<[^>]*>";
return Regex.Replace(content, regexstr, string.Empty, RegexOptions.IgnoreCase);
}
#endregion #region 过滤HTML中的不安全标签
/// <summary>
/// 过滤HTML中的不安全标签
/// </summary>
/// <param name="content"></param>
/// <returns></returns>
public static string RemoveUnsafeHtml(this string content)
{
content = Regex.Replace(content, @"(\<|\s+)o([a-z]+\s?=)", "$1$2", RegexOptions.IgnoreCase);
content = Regex.Replace(content, @"(script|frame|form|meta|behavior|style)([\s|:|>])+", "$1.$2", RegexOptions.IgnoreCase);
return content;
}
#endregion #region 检测是否有Sql危险字符
/// <检测是否有Sql危险字符>
/// 检测是否有Sql危险字符
/// </检测是否有Sql危险字符>
/// <要判断字符串 name="str">要判断字符串</要判断字符串>
/// <判断结果>判断结果</判断结果>
public static bool IsSafeSqlString(this string str)
{
return !Regex.IsMatch(str, @"[-|;|,|\/|\(|\)|\[|\]|\}|\{|%|@|\*|!|\']");
}
#endregion #region 检测是否有危险的可能用于链接的字符串
/// <检测是否有危险的可能用于链接的字符串>
/// 检测是否有危险的可能用于链接的字符串
/// </检测是否有危险的可能用于链接的字符串>
/// <要判断字符串 name="str">要判断字符串</要判断字符串>
/// <判断结果>判断结果</判断结果>
public static bool IsSafeUserInfoString(string str)
{
return !Regex.IsMatch(str, @"^\s*$|^c:\\con\\con$|[%,\*" + "\"" + @"\s\t\<\>\&]|游客|^Guest");
}
#endregion #region 对表 表单内容进行转换HTML操作
/// <对表 表单内容进行转换HTML操作,>
/// 对表 表单内容进行转换HTML操作
/// </对表 表单内容进行转换HTML操作,>
/// <html字符串 name="s">html字符串</html字符串>
public static string HtmlCode(string s)
{
string str = "";
str = s.Replace(">", ">");
str = s.Replace("<", "<");
str = s.Replace(" ", " ");
str = s.Replace("\n", "<br />");
str = s.Replace("\r", "<br />");
str = s.Replace("\r\n", "<br />");
return str;
}
#endregion #region 过滤xss攻击脚本
/// <过滤xss攻击脚本 >
/// 过滤xss攻击脚本
/// </过滤xss攻击脚本 >
/// <传入字符串 name="input">传入字符串</传入字符串>
/// <过滤后的字符串>过滤后的字符串</过滤后的字符串>
public static string FilterXSS(string html)
{
if (string.IsNullOrEmpty(html)) return string.Empty; // CR(0a) ,LF(0b) ,TAB(9) 除外,过滤掉所有的不打印出来字符.
// 目的防止这样形式的入侵 <java\0script>
// 注意:\n, \r, \t 可能需要单独处理,因为可能会要用到
string ret = System.Text.RegularExpressions.Regex.Replace(
html, "([\x00-\x08][\x0b-\x0c][\x0e-\x20])", string.Empty); ret = ret.Replace("\t", ""); //(补充,过滤TAB空格,那也是危险的XSS字符) //替换所有可能的16和10进制构建的恶意代码
//<IMG SRC=@avascript:a&_#X6Cert('XSS')>
ret = System.Text.RegularExpressions.Regex.Replace(ret, @"(&#[x|X]?\d+);?", "", System.Text.RegularExpressions.RegexOptions.IgnoreCase); //过滤Javascript事件触发的恶意代码
string[] keywords = {
"javascript", "vbscript", "expression", "applet", "meta",
"xml", "blink", "script", "embed", "object",
"iframe", "frame", "frameset", "ilayer", "layer", "bgsound", "title", "base",
"onabort", "onactivate", "onafterprint", "onafterupdate", "onbeforeactivate",
"onbeforecopy", "onbeforecut", "onbeforedeactivate", "onbeforeeditfocus", "onbeforepaste",
"onbeforeprint", "onbeforeunload", "onbeforeupdate", "onblur", "onbounce", "oncellchange",
"onchange", "onclick", "oncontextmenu", "oncontrolselect", "oncopy", "oncut", "ondataavailable",
"ondatasetchanged", "ondatasetcomplete", "ondblclick", "ondeactivate", "ondrag", "ondragend",
"ondragenter", "ondragleave", "ondragover", "ondragstart", "ondrop", "onerror", "onerrorupdate",
"onfilterchange", "onfinish", "onfocus", "onfocusin", "onfocusout", "onhelp", "onkeydown",
"onkeypress", "onkeyup", "onlayoutcomplete", "onload", "onlosecapture", "onmousedown",
"onmouseenter", "onmouseleave", "onmousemove", "onmouseout", "onmouseover", "onmouseup",
"onmousewheel", "onmove", "onmoveend", "onmovestart", "onpaste", "onpropertychange",
"onreadystatechange", "onreset", "onresize", "onresizeend", "onresizestart",
"onrowenter", "onrowexit", "onrowsdelete", "onrowsinserted", "onscroll", "onselect",
"onselectionchange", "onselectstart", "onstart", "onstop", "onsubmit", "onunload"}; bool found = true;
while (found)
{
var retBefore = ret;
for (int i = 0; i < keywords.Length; i++)
{
//string pattern = "/"; (补允, 正则前台加/过滤不到)
string pattern = "";
for (int j = 0; j < keywords[i].Length; j++)
{
if (j > 0)
pattern = string.Concat(pattern, '(', "(&#[x|X]0{0,8}([9][a][b]);?)?", "|(�{0,8}([9][10][13]);?)?",
")?");
pattern = string.Concat(pattern, keywords[i][j]);
}
string replacement = string.Concat(keywords[i].Substring(0, 2), "<x>", keywords[i].Substring(2));
ret = System.Text.RegularExpressions.Regex.Replace(ret, pattern, replacement, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
if (ret == retBefore)
found = false;
} }
return ret;
}
#endregion #region 字符串操作 #region 替换指定位置的字符串
/// <summary>
/// 替换指定位置的字符串
/// </summary>
/// <param name="str"></param>
/// <param name="oldStr">被替换的字符串</param>
/// <param name="newStr">替换的字符串</param>
/// <param name="index">第N个出现的地方,从1起编</param>
public static string ReplaceAt(this string str, string oldStr, string newStr, int index)
{
if (oldStr == "")
{
return str;
}
string tmp = str;//字符串的副本
int num = 0, start = 0, end = 0;//已出现次数,替换的起始位置和结束位置
while (true)
{
var _index = tmp.IndexOf(oldStr);//寻找被替换字符串的位置
if (_index != -1)//如果未找到
{
num++;//找到则次数+1
if (num != index)//到了指定的位置
{
//截取字符串的含义:指定字符串的长度以及它第一次出现的位置
tmp = tmp.Substring(_index + oldStr.Length);//去掉该字符串
start += _index + oldStr.Length;//将该字符串的长度设为起始长度
}
else
{
start += _index;
end = oldStr.Length;
break;
}
}
else
{
break;
}
}
if (start != 0 && num == index)
{
str = str.Remove(start, end);
str = str.Insert(start, newStr);
}
return str;
}
#endregion #region 寻找指定字符在某字符串中出现的次数
/// <summary>
/// 寻找指定字符在某字符串中出现的次数
/// </summary>
/// <param name="str"></param>
/// <param name="oldStr">被寻找的字符串</param>
/// <returns></returns>
public static int Find(this string str, string oldStr)
{
var start = 0;
var num = 0;
while (true)
{
var _index = str.IndexOf(oldStr, start);//寻找被替换字符串的位置
if (_index != -1)//如果找到
{
num++;//找到则次数+1
start = _index + oldStr.Length;//将该字符串的长度设为起始长度
}
else //如果找不到,则退出
{
break;
}
}
return start != 0 ? num : -1;
}
#endregion #endregion #endregion #region 非拓展方法 #region 正则读取xml字符串中属性的值
/// <summary>
/// 正则读取xml字符串中属性的值
/// </summary>
/// <param name="s">需要读取的xml字符串</param>
/// <param name="value">需要读取的属性</param>
/// <returns></returns>
public static string RegAttrValue(string s, string value)
{
Regex r = new Regex(value + "=\"[^\"]*\"");
StringBuilder sb = new StringBuilder(r.Match(s).ToString());
return sb.Replace("\"", "").ToString().Replace(value, "").ToString().Replace("=", "").ToString();
}
#endregion #region 正则读取xml字符串中结点的值
/// <summary>
/// 正则读取xml字符串中结点的值
/// </summary>
/// <param name="s">需要读取的xml字符串</param>
/// <param name="value">需要读取的结点</param>
/// <returns></returns>
public static string RegNodeValue(string s, string value)
{
Regex r = new Regex("<" + value + ">.*" + "</" + value + ">");
StringBuilder sb = new StringBuilder(r.Match(s).ToString());
return sb.Replace("<" + value + ">", "").Replace("</" + value + ">", "").ToString();
}
#endregion #region 转全角的函数(SBC case)
/// <summary>
/// 转全角的函数(SBC case)
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static string ToSBC(string input)
{
//半角转全角:
char[] c = input.ToCharArray();
for (int i = 0; i < c.Length; i++)
{
if (c[i] == 32)
{
c[i] = (char)12288;
continue;
}
if (c[i] < 127)
c[i] = (char)(c[i] + 65248);
}
return new string(c);
} #endregion #region 转半角的函数(SBC case) /// <summary>
/// 转半角的函数(SBC case)
/// </summary>
/// <param name="input">输入</param>
/// <returns></returns>
public static string ToDBC(string input)
{
char[] c = input.ToCharArray();
for (int i = 0; i < c.Length; i++)
{
if (c[i] == 12288)
{
c[i] = (char)32;
continue;
}
if (c[i] > 65280 && c[i] < 65375)
c[i] = (char)(c[i] - 65248);
}
return new string(c);
}
#endregion #region 从字符串里随机得到,规定个数的字符串. /// <summary>
/// 从字符串里随机得到,规定个数的字符串.
/// </summary>
/// <param name="allChar"></param>
/// <param name="CodeCount"></param>
/// <returns></returns>
private static string GetRandomCode(string allChar, int CodeCount)
{
//string allChar = "1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,i,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";
string[] allCharArray = allChar.Split(',');
string RandomCode = "";
int temp = -1;
Random rand = new Random();
for (int i = 0; i < CodeCount; i++)
{
if (temp != -1)
{
rand = new Random(temp * i * ((int)DateTime.Now.Ticks));
} int t = rand.Next(allCharArray.Length - 1); while (temp == t)
{
t = rand.Next(allCharArray.Length - 1);
} temp = t;
RandomCode += allCharArray[t];
}
return RandomCode;
} #endregion #region 转换人民币大小金额
/// <summary>
/// 转换人民币大小金额
/// </summary>
/// <param name="num">金额</param>
/// <returns>返回大写形式</returns>
public static string CmycurD(decimal num)
{
string str1 = "零壹贰叁肆伍陆柒捌玖"; //0-9所对应的汉字
string str2 = "万仟佰拾亿仟佰拾万仟佰拾元角分"; //数字位所对应的汉字
string str3 = ""; //从原num值中取出的值
string str4 = ""; //数字的字符串形式
string str5 = ""; //人民币大写金额形式
int i; //循环变量
int j; //num的值乘以100的字符串长度
string ch1 = ""; //数字的汉语读法
string ch2 = ""; //数字位的汉字读法
int nzero = 0; //用来计算连续的零值是几个
int temp; //从原num值中取出的值 num = Math.Round(Math.Abs(num), 2); //将num取绝对值并四舍五入取2位小数
str4 = ((long)(num * 100)).ToString(); //将num乘100并转换成字符串形式
j = str4.Length; //找出最高位
if (j > 15) { return "溢出"; }
str2 = str2.Substring(15 - j); //取出对应位数的str2的值。如:200.55,j为5所以str2=佰拾元角分 //循环取出每一位需要转换的值
for (i = 0; i < j; i++)
{
str3 = str4.Substring(i, 1); //取出需转换的某一位的值
temp = Convert.ToInt32(str3); //转换为数字
if (i != (j - 3) && i != (j - 7) && i != (j - 11) && i != (j - 15))
{
//当所取位数不为元、万、亿、万亿上的数字时
if (str3 == "0")
{
ch1 = "";
ch2 = "";
nzero = nzero + 1;
}
else
{
if (str3 != "0" && nzero != 0)
{
ch1 = "零" + str1.Substring(temp * 1, 1);
ch2 = str2.Substring(i, 1);
nzero = 0;
}
else
{
ch1 = str1.Substring(temp * 1, 1);
ch2 = str2.Substring(i, 1);
nzero = 0;
}
}
}
else
{
//该位是万亿,亿,万,元位等关键位
if (str3 != "0" && nzero != 0)
{
ch1 = "零" + str1.Substring(temp * 1, 1);
ch2 = str2.Substring(i, 1);
nzero = 0;
}
else
{
if (str3 != "0" && nzero == 0)
{
ch1 = str1.Substring(temp * 1, 1);
ch2 = str2.Substring(i, 1);
nzero = 0;
}
else
{
if (str3 == "0" && nzero >= 3)
{
ch1 = "";
ch2 = "";
nzero = nzero + 1;
}
else
{
if (j >= 11)
{
ch1 = "";
nzero = nzero + 1;
}
else
{
ch1 = "";
ch2 = str2.Substring(i, 1);
nzero = nzero + 1;
}
}
}
}
}
if (i == (j - 11) || i == (j - 3))
{
//如果该位是亿位或元位,则必须写上
ch2 = str2.Substring(i, 1);
}
str5 = str5 + ch1 + ch2; if (i == j - 1 && str3 == "0")
{
//最后一位(分)为0时,加上“整”
str5 = str5 + '整';
}
}
if (num == 0)
{
str5 = "零元整";
}
return str5;
} /**/
/// <summary>
/// 一个重载,将字符串先转换成数字在调用CmycurD(decimal num)
/// </summary>
/// <param name="num">用户输入的金额,字符串形式未转成decimal</param>
/// <returns></returns>
public static string CmycurD(string numstr)
{
try
{
decimal num = Convert.ToDecimal(numstr);
return CmycurD(num);
}
catch
{
return "非数字形式!";
}
} #endregion #region 汉字转换为拼音
private static int[] pyValue = new int[]
{
-20319,-20317,-20304,-20295,-20292,-20283,-20265,-20257,-20242,-20230,-20051,-20036,
-20032,-20026,-20002,-19990,-19986,-19982,-19976,-19805,-19784,-19775,-19774,-19763,
-19756,-19751,-19746,-19741,-19739,-19728,-19725,-19715,-19540,-19531,-19525,-19515,
-19500,-19484,-19479,-19467,-19289,-19288,-19281,-19275,-19270,-19263,-19261,-19249,
-19243,-19242,-19238,-19235,-19227,-19224,-19218,-19212,-19038,-19023,-19018,-19006,
-19003,-18996,-18977,-18961,-18952,-18783,-18774,-18773,-18763,-18756,-18741,-18735,
-18731,-18722,-18710,-18697,-18696,-18526,-18518,-18501,-18490,-18478,-18463,-18448,
-18447,-18446,-18239,-18237,-18231,-18220,-18211,-18201,-18184,-18183, -18181,-18012,
-17997,-17988,-17970,-17964,-17961,-17950,-17947,-17931,-17928,-17922,-17759,-17752,
-17733,-17730,-17721,-17703,-17701,-17697,-17692,-17683,-17676,-17496,-17487,-17482,
-17468,-17454,-17433,-17427,-17417,-17202,-17185,-16983,-16970,-16942,-16915,-16733,
-16708,-16706,-16689,-16664,-16657,-16647,-16474,-16470,-16465,-16459,-16452,-16448,
-16433,-16429,-16427,-16423,-16419,-16412,-16407,-16403,-16401,-16393,-16220,-16216,
-16212,-16205,-16202,-16187,-16180,-16171,-16169,-16158,-16155,-15959,-15958,-15944,
-15933,-15920,-15915,-15903,-15889,-15878,-15707,-15701,-15681,-15667,-15661,-15659,
-15652,-15640,-15631,-15625,-15454,-15448,-15436,-15435,-15419,-15416,-15408,-15394,
-15385,-15377,-15375,-15369,-15363,-15362,-15183,-15180,-15165,-15158,-15153,-15150,
-15149,-15144,-15143,-15141,-15140,-15139,-15128,-15121,-15119,-15117,-15110,-15109,
-14941,-14937,-14933,-14930,-14929,-14928,-14926,-14922,-14921,-14914,-14908,-14902,
-14894,-14889,-14882,-14873,-14871,-14857,-14678,-14674,-14670,-14668,-14663,-14654,
-14645,-14630,-14594,-14429,-14407,-14399,-14384,-14379,-14368,-14355,-14353,-14345,
-14170,-14159,-14151,-14149,-14145,-14140,-14137,-14135,-14125,-14123,-14122,-14112,
-14109,-14099,-14097,-14094,-14092,-14090,-14087,-14083,-13917,-13914,-13910,-13907,
-13906,-13905,-13896,-13894,-13878,-13870,-13859,-13847,-13831,-13658,-13611,-13601,
-13406,-13404,-13400,-13398,-13395,-13391,-13387,-13383,-13367,-13359,-13356,-13343,
-13340,-13329,-13326,-13318,-13147,-13138,-13120,-13107,-13096,-13095,-13091,-13076,
-13068,-13063,-13060,-12888,-12875,-12871,-12860,-12858,-12852,-12849,-12838,-12831,
-12829,-12812,-12802,-12607,-12597,-12594,-12585,-12556,-12359,-12346,-12320,-12300,
-12120,-12099,-12089,-12074,-12067,-12058,-12039,-11867,-11861,-11847,-11831,-11798,
-11781,-11604,-11589,-11536,-11358,-11340,-11339,-11324,-11303,-11097,-11077,-11067,
-11055,-11052,-11045,-11041,-11038,-11024,-11020,-11019,-11018,-11014,-10838,-10832,
-10815,-10800,-10790,-10780,-10764,-10587,-10544,-10533,-10519,-10331,-10329,-10328,
-10322,-10315,-10309,-10307,-10296,-10281,-10274,-10270,-10262,-10260,-10256,-10254
}; private static string[] pyName = new string[]
{
"A","Ai","An","Ang","Ao","Ba","Bai","Ban","Bang","Bao","Bei","Ben",
"Beng","Bi","Bian","Biao","Bie","Bin","Bing","Bo","Bu","Ba","Cai","Can",
"Cang","Cao","Ce","Ceng","Cha","Chai","Chan","Chang","Chao","Che","Chen","Cheng",
"Chi","Chong","Chou","Chu","Chuai","Chuan","Chuang","Chui","Chun","Chuo","Ci","Cong",
"Cou","Cu","Cuan","Cui","Cun","Cuo","Da","Dai","Dan","Dang","Dao","De",
"Deng","Di","Dian","Diao","Die","Ding","Diu","Dong","Dou","Du","Duan","Dui",
"Dun","Duo","E","En","Er","Fa","Fan","Fang","Fei","Fen","Feng","Fo",
"Fou","Fu","Ga","Gai","Gan","Gang","Gao","Ge","Gei","Gen","Geng","Gong",
"Gou","Gu","Gua","Guai","Guan","Guang","Gui","Gun","Guo","Ha","Hai","Han",
"Hang","Hao","He","Hei","Hen","Heng","Hong","Hou","Hu","Hua","Huai","Huan",
"Huang","Hui","Hun","Huo","Ji","Jia","Jian","Jiang","Jiao","Jie","Jin","Jing",
"Jiong","Jiu","Ju","Juan","Jue","Jun","Ka","Kai","Kan","Kang","Kao","Ke",
"Ken","Keng","Kong","Kou","Ku","Kua","Kuai","Kuan","Kuang","Kui","Kun","Kuo",
"La","Lai","Lan","Lang","Lao","Le","Lei","Leng","Li","Lia","Lian","Liang",
"Liao","Lie","Lin","Ling","Liu","Long","Lou","Lu","Lv","Luan","Lue","Lun",
"Luo","Ma","Mai","Man","Mang","Mao","Me","Mei","Men","Meng","Mi","Mian",
"Miao","Mie","Min","Ming","Miu","Mo","Mou","Mu","Na","Nai","Nan","Nang",
"Nao","Ne","Nei","Nen","Neng","Ni","Nian","Niang","Niao","Nie","Nin","Ning",
"Niu","Nong","Nu","Nv","Nuan","Nue","Nuo","O","Ou","Pa","Pai","Pan",
"Pang","Pao","Pei","Pen","Peng","Pi","Pian","Piao","Pie","Pin","Ping","Po",
"Pu","Qi","Qia","Qian","Qiang","Qiao","Qie","Qin","Qing","Qiong","Qiu","Qu",
"Quan","Que","Qun","Ran","Rang","Rao","Re","Ren","Reng","Ri","Rong","Rou",
"Ru","Ruan","Rui","Run","Ruo","Sa","Sai","San","Sang","Sao","Se","Sen",
"Seng","Sha","Shai","Shan","Shang","Shao","She","Shen","Sheng","Shi","Shou","Shu",
"Shua","Shuai","Shuan","Shuang","Shui","Shun","Shuo","Si","Song","Sou","Su","Suan",
"Sui","Sun","Suo","Ta","Tai","Tan","Tang","Tao","Te","Teng","Ti","Tian",
"Tiao","Tie","Ting","Tong","Tou","Tu","Tuan","Tui","Tun","Tuo","Wa","Wai",
"Wan","Wang","Wei","Wen","Weng","Wo","Wu","Xi","Xia","Xian","Xiang","Xiao",
"Xie","Xin","Xing","Xiong","Xiu","Xu","Xuan","Xue","Xun","Ya","Yan","Yang",
"Yao","Ye","Yi","Yin","Ying","Yo","Yong","You","Yu","Yuan","Yue","Yun",
"Za", "Zai","Zan","Zang","Zao","Ze","Zei","Zen","Zeng","Zha","Zhai","Zhan",
"Zhang","Zhao","Zhe","Zhen","Zheng","Zhi","Zhong","Zhou","Zhu","Zhua","Zhuai","Zhuan",
"Zhuang","Zhui","Zhun","Zhuo","Zi","Zong","Zou","Zu","Zuan","Zui","Zun","Zuo"
}; /// <summary>
/// 把汉字转换成拼音(全拼)
/// </summary>
/// <param name="hzString">汉字字符串</param>
/// <param name="IsFiltrate">是否过滤多余的</param>
/// <returns>转换后的拼音(全拼)字符串</returns>
public static string ConvertToPinYin(string hzString, bool IsFiltrate)
{
// 匹配中文字符
Regex regex = new Regex("^[\u4e00-\u9fa5]$");
byte[] array = new byte[2];
StringBuilder pyString = new StringBuilder();
int chrAsc = 0;
int i1 = 0;
int i2 = 0;
char[] noWChar = hzString.ToCharArray(); for (int j = 0; j < noWChar.Length; j++)
{
// 中文字符
if (regex.IsMatch(noWChar[j].ToString()))
{
array = System.Text.Encoding.Default.GetBytes(noWChar[j].ToString());
i1 = (short)(array[0]);
i2 = (short)(array[1]);
chrAsc = i1 * 256 + i2 - 65536;
if (chrAsc > 0 && chrAsc < 160)
{
pyString.Append(noWChar[j]);
}
else
{
// 修正部分文字
if (chrAsc == -9254) // 修正“圳”字
pyString.Append("Zhen");
else
{
for (int i = (pyValue.Length - 1); i >= 0; i--)
{
if (pyValue[i] <= chrAsc)
{
pyString.Append(pyName[i]);
break;
}
}
}
}
}
// 非中文字符
else
{
if (IsFiltrate == true)
{
//过滤掉非英文非数字的字符
Regex filtrateRegex = new Regex("^[A-Za-z0-9]+$");
if (filtrateRegex.IsMatch(noWChar[j].ToString()))
{
pyString.Append(noWChar[j].ToString());
}
else
{
pyString.Append("-");
}
}
else
{ pyString.Append(noWChar[j].ToString());
}
}
}
return pyString.ToString();
}
#endregion #region 取随机数(数字)
/// <summary>
/// 取随机数(数字)
/// </summary>
/// <param name="length">随机数长度</param>
/// <returns></returns>
public static string BuildRandomStr(int length)
{
Random rand = new Random();
int num = rand.Next();
string str = num.ToString();
if (str.Length > length)
{
str = str.Substring(0, length);
}
else if (str.Length < length)
{
int n = length - str.Length;
while (n > 0)
{
str += "2";
n--;
}
}
return str;
}
#endregion #region 生成随机字母字符串(数字字母混和)
/// <summary>
/// 生成随机字母字符串(数字字母混和)
/// </summary>
/// <param name="codeCount">待生成的位数</param>
/// <param name="rep">遍历生成随机传遍历当前数</param>
/// <returns>生成的字母字符串</returns>
public static string GenerateCheckCode(int codeCount, int rep)
{
string str = string.Empty;
long num2 = DateTime.Now.Ticks + rep;
Random random = new Random(((int)(((ulong)num2) & 0xffffffffL)) | ((int)(num2 >> rep)));
for (int i = 0; i < codeCount; i++)
{
char ch;
int num = random.Next();
if ((num % 2) == 0)
{
ch = (char)(0x30 + ((ushort)(num % 10)));
}
else
{
ch = (char)(0x41 + ((ushort)(num % 0x1a)));
}
str = str + ch.ToString();
}
return str;
}
#endregion #endregion
}
}
涵盖网站基本使用的正则表达式的验证方法.cs的更多相关文章
- textbox只能输入数字或中文的常用正则表达式和验证方法
验证数字的正则表达式集 验证数字:^[0-9]*$ 验证n位的数字:^\d{n}$ 验证至少n位数字:^\d{n,}$ 验证m-n位的数字:^\d{m,n}$ 验证零和非零开头的数字:^(0|[1-9 ...
- jquery validate.addMethod 正则表达式 (自定义验证方法)
项目中使用的jQuery添加的校验的方法 $(document).ready(function(){ 5 6/* 设置默认属性 */ 7$.vali ...
- ASP.NET开发中主要的字符验证方法-JS验证、正则表达式、验证控件、后台验证
ASP.NET开发中主要的字符验证方法-JS验证.正则表达式.验证控件.后台验证 2012年03月19日 星期一 下午 8:53 在ASP.NET开发中主要的验证方法收藏 <1>使用JS验 ...
- 教你50招提升ASP.NET性能(十八):在处理网站性能问题前,首先验证问题是否出在客户端
(29)Before tackling any website performance issue, first verify the problem isn’t on the client 招数29 ...
- Java国际化号码验证方法,国内手机号正则表达式
Java国际化号码验证方法,国内手机号正则表达式 中国电信号段 133.149.153.173.177.180.181.189.199 中国联通号段 130.131.132.145.155.156.1 ...
- java.util.regex.Pattern正则表达式写验证器示例
import java.util.regex.Pattern; /** * 校验器:利用正则表达式校验邮箱.手机号等 * */ public class Validator { /** * 正则表达式 ...
- js进阶正则表达式15验证身份证号(|符号的使用:var reg=/^\d{17}[\d|X]$|^\d{15}$/)(str的方法substr)
js进阶正则表达式15验证身份证号(|符号的使用:var reg=/^\d{17}[\d|X]$|^\d{15}$/)(str的方法substr) 一.总结 1.|符号的使用:var reg=/^\d ...
- js进阶正则表达式14验证邮编(input的pattern属性)(正则表达式加起^始$)
js进阶正则表达式14验证邮编(input的pattern属性)(正则表达式加起^始$) 一.总结 1.input的pattern属性:里面可以直接放正则表达式,<input type=&quo ...
- 从零开始实现asp.net MVC4框架网站的用户登录以及权限验证模块 详细教程
从零开始实现asp.net MVC4框架网站的用户登录以及权限验证模块 详细教程 用户登录与权限验证是网站不可缺少的一部分功能,asp.net MVC4框架内置了用于实现该功能的类库,只需要简单搭 ...
随机推荐
- Sqli-labs less 63
Less-63 和less62一致,我们只需要看到sql语句上 $sql="SELECT * FROM security.users WHERE id='$id' LIMIT 0,1&quo ...
- mysql修改数据库表权限
ps:通常我用的是:1.“grant all on *.* to root@'%' identified by 'yourpassword';”——这个还可以顺带设置密码.2.“flush privi ...
- HDU5008 Boring String Problem(后缀数组)
练习一下字符串,做一下这道题. 首先是关于一个字符串有多少不同子串的问题,串由小到大排起序来应该是按照sa[i]的顺序排出来的产生的. 好像abbacd,排序出来的后缀是这样的 1---abbacd ...
- CSS Devices可以让你在线直接获取使用CSS写的Mobile外形。
CSS Devices可以让你在线直接获取使用CSS写的Mobile外形. CSS Devices 彩蛋爆料直击现场
- 作品展示,JavaScript 版水果忍者
点这里 <水果忍者>是一款非常受喜欢的手机游戏,刚看到新闻说<水果忍者>四周年新版要上线了.网页版的切水果游戏由百度 JS 小组开发,采用 vml + svg 绘图,使用了 R ...
- sshpass
示例: ./sshpass -p ‘123456’ ssh -o StrictHostKeyChecking=no root@192.168.1.15 ./sshpass -p ‘123456 ...
- 斐波那契数列公式算法-JS实现
之前算斐波那契数列都是算前两个数相加实现的 比如0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233,377,610,987,1597,2584,4181 ...
- SWD应用接口
随着ARM公司对Cortex系列的推出,采样SWD方式调试成了大家的首选.SWD不仅速度可以与JTAG媲美,而且使用的调试线少得多.很多人在采样SWD方式进行调试时,一般都是采用4线: ① VCC ...
- log4j打印日志配置
1.所需的jar包 <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifa ...
- TCP释放连接的四次挥手过程
TCP断开连接的过程:TCP四次挥手. 数据传输结束后,通信的双方都可释放连接.现在A和B都处于ESTABLISHED状态.A的应用进程先向TCP发出连接释放报文段,主动关闭TCP连接.A把连接释放报 ...