public static class StringHelper { // 分数、带分数 public static string sMatchNumInch = @" (([1-9]\d*-)?[1-9]\d*/[1-9]\d*|[1-9]\d*)"" " ; // 数字 public static string sMatchNum = @" ((-|)([1-9]+\d*|0)(\.\d+)?|0) " ; // 整数 public
public static class StringHelper { //分数、带分数 public static string sMatchNumInch = @"(([1-9]\d*-)?[1-9]\d*/[1-9]\d*|[1-9]\d*)"""; //数字 public static string sMatchNum = @"((-|)([1-9]+\d*|0)(\.\d+)?|0)"; //整数 public static string sMatchNumInt = @"((|-)[1-9]+(\d+)?|0)"; //正整数 public static string sMatchNumIntPositive = @"([1-9]+(\d+)?)"; //负整数 public static string sMatchNumIntNegative = @"(-[1-9]+(\d+)?)"; //非负整数 public static string sMatchNumIntNonNegative = @"([1-9]+(\d+)?|0)"; //非正整数 public static string sMatchNumIntNonPositive = @"(-[1-9]+(\d+)?|0)"; //正数 public static string sMatchNumPositive = @"(([1-9]+\d*)?((|0)\.\d+)?)"; //负数 public static string sMatchNumNegative = @"(-([1-9]+\d*|0)(\.\d+)?)"; //非负数 public static string sMatchNumNonNegative = @"(([1-9]+\d*)?((|0)\.\d+)?)"; //非正数 public static string sMatchNumNonPositive = @"(-([1-9]+\d*|0)(\.\d+)?|0)"; /// <summary> /// 判断字符串是否是数字 /// </summary> /// <param name="str"></param> /// <param name="nt"></param> /// <returns></returns> /// <summary> public static bool IsNumType(this string str, NumType nt) { if (str == "") return false; string matchString = ""; switch (nt) { case NumType.Number: //数字 matchString = sMatchNum; break; case NumType.PositiveInt: //正整数 matchString = sMatchNumIntPositive; break; case NumType.NonNegativeInt: //非负整数(正整数 + 0) matchString = sMatchNumIntNonNegative; break; case NumType.NonPositiveInt: //非正整数(负整数 + 0) matchString = sMatchNumIntNonPositive; break; case NumType.NegativeInt: //负整数 matchString = sMatchNumIntNegative; break; case NumType.Int: //整数 matchString = sMatchNumInt; break; case NumType.NonNegative: //非负数(正数 + 0) matchString = sMatchNumNonNegative; break; case NumType.Positive: //正数 matchString = sMatchNumPositive; break; case NumType.NonPositive: //非正数(负数 + 0) matchString = sMatchNumNonPositive; break; case NumType.Negative: //负数 matchString = sMatchNumNegative; break; default: break; } return Regex.IsMatch(str, matchString, RegexOptions.IgnoreCase); } /// <summary> /// 第1位:9表示“非”,1表示“数”,2表示“整数” /// 末位:1表示正,0表示负 /// </summary> public enum NumType { /// <summary> /// 浮点数 /// </summary> Number = 1, /// <summary> /// 整数 /// </summary> Int = 2, /// <summary> /// 正数 /// </summary> Positive = 11, /// <summary> /// 负数 /// </summary> Negative = 10, /// <summary> /// 正整数 /// </summary> PositiveInt = 21, /// <summary> /// 负整数 /// </summary> NegativeInt = 20, /// <summary> /// 非正数 /// </summary> NonPositive = 911, /// <summary> /// 非负数 /// </summary> NonNegative = 910, /// <summary> /// 非正整数 /// </summary> NonPositiveInt = 921, /// <summary> /// 非负整数 /// </summary> NonNegativeInt = 920, } }