zoukankan      html  css  js  c++  java
  • 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, } }
  • 相关阅读:
    ThinkPHP 中入口文件中的APP_DEBUG为TRUE时不报错,改为FALSE时报错
    ASP.NET LinqDataSource数据绑定后,遇到[MissingMethodException: 没有为该对象定义无参数的构造函数。]问题。
    error 2593 operator << 不明确的可能的解决方法
    C++读取mysql中utf8mb4编码表数据乱码问题及UTF8转GBK编码
    arcgis flexviewer中由Application向widget传值
    Flash Builder中“Error: #2036 加载未完成”错误的解决方法
    ArcGIS Server API for JavaScript调用错误:已阻止跨源请求:同源策略禁止读取位于......
    Win10下E3-1231 V3开启Intel虚拟化技术(vt-x)安装HAXM
    GPS accuracy in Android
    解决Win7旗舰版开机后无线网络识别非常慢的问题
  • 原文地址:https://www.cnblogs.com/swtool/p/11597486.html
Copyright © 2011-2022 走看看