zoukankan      html  css  js  c++  java
  • .Net常用正则判断方法

            /// <summary>
            ///  判断string类型否为数字
            /// </summary>
            /// <param name="strNumber"></param>
            /// <returns></returns>
            public static bool IsNumber(string strNumber)
            {
                string strValidRealPattern = "^([-]|[.]|[-.]|[0-9])[0-9]*[.]*[0-9]+$";
                string strValidIntegerPattern = "^([-]|[0-9])[0-9]*$";
                return !Regex.IsMatch(strNumber, "[^0-9.-]") &&
                       !Regex.IsMatch(strNumber, "[0-9]*[.][0-9]*[.][0-9]*") &&
                       !Regex.IsMatch(strNumber, "[0-9]*[-][0-9]*[-][0-9]*") &&
                       Regex.IsMatch(strNumber, "(" + strValidRealPattern + ")|(" + strValidIntegerPattern + ")");
            }
    
            /// <summary>
            /// 判断string类型否为正整数+0
            /// </summary>
            /// <param name="strNumber"></param>
            /// <returns></returns>
            public static bool IsPositive(string strNumber) => Regex.IsMatch(strNumber, "^\d+$");
    
    
            /// <summary>
            /// 判断string类型否为金额
            /// </summary>
            /// <param name="strNumber"></param>
            /// <returns></returns>
            public static bool IsAmount(string strNumber) => Regex.IsMatch(strNumber, "^[0-9]+(.[0-9]{2})?$");
    
    
            /// <summary>
            /// 判断string类型否为手机号
            /// </summary>
            /// <param name="strPhone"></param>
            /// <returns></returns>
            public static bool IsPhone(string strPhone) => Regex.IsMatch(strPhone, "^0?(13[0-9]|15[012356789]|18[012356789]|14[012356789]|17[012356789])[0-9]{8}$");
    
    
            /// <summary>
            /// 判断string类型否为固定电话号
            /// </summary>
            /// <param name="strTel"></param>
            /// <returns></returns>
            public static bool IsTel(string strTel) => Regex.IsMatch(strTel, "^(0[0-9]{2,3}-)?([2-9][0-9]{6,7})+(-[0-9]{1,4})?$");
    
    
            /// <summary>
            /// 判断string类型否为邮编
            /// </summary>
            /// <param name="strZipCode"></param>
            /// <returns></returns>
            public static bool IsZipCode(string strZipCode) => Regex.IsMatch(strZipCode, "[0-9]{6}");
    
    
            /// <summary>
            /// 判断string类型否为Email
            /// </summary>
            /// <param name="strEmail"></param>
            /// <returns></returns>
            public static bool IsEmail(string strEmail) => Regex.IsMatch(strEmail, "\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
    
    
            /// <summary>
            /// 判断是否为日期
            /// </summary>
            /// <param name="dateStr"></param>
            /// <returns></returns>
            public static bool IsDateString(string dateStr) => DateTime.TryParse(dateStr, out var date);
    View Code
  • 相关阅读:
    Elasticsearch(ES) 创建索引
    Elasticsearch(ES) 下载&安装
    一文带您了解 Elasticsearch 中,如何进行索引管理(图文教程)
    Spring Boot 2.0 快速集成整合消息中间件 Kafka
    一文教您如何通过 Docker 搭建反向代理 Ngnix,并配置 Https SSL 证书
    Django开发文档-域用户集成登录
    Python实现按键精灵(二)-找图找色
    Python学习笔记-SQLSERVER的大批量导入以及日常操作(比executemany快3倍)
    Python爬虫案例-获取最新的中国行政区域划分
    PostgreSQL自动更新序列sequence
  • 原文地址:https://www.cnblogs.com/heheblog/p/10270564.html
Copyright © 2011-2022 走看看