AutoCAD.Net/C#.Net QQ群:193522571 各种数字的正则表达式类
public static class StringHelper { static string sXiaoShuBuFen =@"(\.\d+)?";
//分数、带分数
public static string sMatchNumInch = @"(([1-9]\d*-)?[1-9]\d*/[1-9]\d*|[1-9]\d*)""";
//正整数
public static string sMatchNumIntPositive = @"[1-9]\d*";
//整数
public static string sMatchNumInt = string.Format(@"(-?{0}|0)", sMatchNumIntPositive);
//数字
//public static string sMatchNum = @"(-?([1-9]\d*|0))(\.\d+)?";
public static string sMatchNum = string.Format(@"{0}{1}", sMatchNumInt, sXiaoShuBuFen);
//负整数
public static string sMatchNumIntNegative = string.Format(@"-{0}", sMatchNumIntPositive);
//非负整数
public static string sMatchNumIntNonNegative = string.Format(@"({0}|0)", sMatchNumIntPositive);
//非正整数有问题,暂时不要了
public static string sMatchNumIntNonPositive = string.Format(@"^(-{0}|0)$", sMatchNumIntPositive);
//正数
//统一不好的是识别001
//public static string sMatchNumPositive = string.Format(@"[1-9]\d*.\d*|0.\d*[1-9]\d*");
public static string sMatchNumPositive = string.Format(@"({0}{1}|0(\.\d+))", sMatchNumIntPositive,sXiaoShuBuFen);
//负数
public static string sMatchNumNegative = string.Format(@"-({0})", sMatchNumPositive);
//非负数
public static string sMatchNumNonNegative = string.Format(@"({0}|0)", sMatchNumPositive);
//非正数有问题,暂时不要了
//public static string sMatchNumNonPositive = string.Format(@"^({0}|0)$", sMatchNumNegative);/// <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, } }