惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

J
Java Code Geeks
腾讯CDC
Jina AI
Jina AI
博客园 - 司徒正美
博客园 - 三生石上(FineUI控件)
Apple Machine Learning Research
Apple Machine Learning Research
GbyAI
GbyAI
WordPress大学
WordPress大学
Hugging Face - Blog
Hugging Face - Blog
T
The Blog of Author Tim Ferriss
小众软件
小众软件
M
MIT News - Artificial intelligence
MyScale Blog
MyScale Blog
D
Docker
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Google DeepMind News
Google DeepMind News
月光博客
月光博客
L
LangChain Blog
F
Fortinet All Blogs
Microsoft Azure Blog
Microsoft Azure Blog
博客园 - Franky
C
Check Point Blog
U
Unit 42
人人都是产品经理
人人都是产品经理

博客园 - 书生

乐在其中设计模式(C#)系列文章索引 C#事件(event)解析 C#之委托的个人理解 NUnit2.0使用简单教程 如何将自己的代码自动添加版权信息的及其扩展 - 书生 - 博客园 浅谈.NET中的数据绑定表达式(二) - 书生 - 博客园 经典SQL语句集锦(收藏版) 获得字符串长度(中文字符占2) - 书生 - 博客园 Community Server的HttpHandler .NET继承本质论 如何将自己的代码自动添加版权信息 谈谈C#中的三个关键词new , virtual , override VS中常见的扩展名,看看你知道几个? 关于数据绑定表达式 读写xml节点的数据总结 Asp.net(C#)实现验证码功能 自定义编码解析函数示例 将自定义对象序列化为XML字符串 设置或者得到CheckBoxList选中了的值
常用正则表达式,已封装为静态方法,
书生 · 2007-05-26 · via 博客园 - 书生

#region Regular Expression

       
/// <summary>
       
/// 校验字符串是否只包含字母与数字
       
/// </summary>
       
/// <param name="toVerified">需要校验的字符串</param>
       
/// <returns>true表示符合要求,false表示不符合要求</returns>

        public static bool IsOnlyLetterAndDigit(string toVerified)
       
{
            Regex rx
= new Regex(@"^[a-zA-Z0-9-]*$");
           
return rx.IsMatch(toVerified.Trim(), 0);
        }


       
/// <summary>
       
/// 检验是否是整数
       
/// </summary>
       
/// <param name="str">需要检验的字符串</param>
       
/// <returns>是否为整数:true是整数,false非整数</returns>

        public static bool IsInt(string str)
       
{
            Regex rx
= new Regex(@"^[0123456789]+$");
           
return rx.IsMatch(str);
        }


       
/// <summary>
       
/// 校验是否为正的浮点数
       
/// </summary>
       
/// <param name="price">需要检验的字符串</param>
       
/// <returns>是否为正浮点,是返回true,否则返回false</returns>

        public static bool IsFloat(string str)
       
{
            Regex rx
= new Regex(@"^[0-9]*(.)?[0-9]+$", RegexOptions.IgnoreCase);
           
return rx.IsMatch(str.Trim());   
        }


       
/// <summary>
       
/// 检验是否为数字
       
/// </summary>
       
/// <param name="str">需要检验的字符串</param>
       
/// <returns>是否为数字:true代表是,false代表否</returns>

        public static bool IsNumber(string str)
       
{

            Regex rx
= new Regex(@"^[+-]?[0123456789]*[.]?[0123456789]*$");
           
return rx.IsMatch(str);
        }


       
/// <summary>
       
/// 检验字符串是否为日期时间
       
/// </summary>
       
/// <param name="str">需要检验的字符串</param>
       
/// <returns>是否为日期时间:true代表是,false代表否</returns>

        public static bool IsDateTime(string str)
       
{
            Regex rx
= new Regex(@"^[ ]*[012 ]?[0123456789]?[0123456789]{2}[ ]*[-]{1}[ ]*[01]?[0123456789]{1}[ ]*[-]{1}[ ]*[0123]?[0123456789]{1}[ ]*[012]?[0123456789]{1}[ ]*[:]{1}[ ]*[012345]?[0123456789]{1}[ ]*[:]{1}[ ]*[012345]?[0123456789]{1}[ ]*$");
           
return rx.IsMatch(str);
        }


       
/// <summary>
       
/// 检验字符串是否为邮政编码
       
/// </summary>
       
/// <param name="str">需要检验的字符串</param>
       
/// <returns>是否为邮政编码:true代表是,false代表否</returns>

        public static bool IsPostCode(string str)
       
{
            Regex rx
= new Regex(@"^[0123456789]{6}$");
           
return rx.IsMatch(str);
        }


       
/// <summary>
       
/// 检验字符串是否为身份证号
       
/// </summary>
       
/// <param name="str">需要检验的字符串</param>
       
/// <returns>是否为身份证号:true代表是,false代表否</returns>

        public static bool IsCode(string str)
       
{
            Regex rx
= new Regex(@"^[0123456789]{15,18}$");
           
return rx.IsMatch(str);
        }


       
/// <summary>
       
/// 检验字符串是否为电子邮件
       
/// </summary>
       
/// <param name="str">需要检验的字符串</param>
       
/// <returns>是否为电子邮件:true代表是,false代表否</returns>

        public static bool IsEMail(string str)
       
{
            Regex rx
= new Regex(@"w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*");
           
return rx.IsMatch(str);
        }


       
/// <summary>
       
/// 检验字符串是否为中国地区的电话号码
       
/// </summary>
       
/// <param name="str">需要检验的字符串</param>
       
/// <returns>是否为中国地区的电话号码:true代表是,false代表否</returns>

        public static bool IsPhoneNumber(string str)
       
{
            Regex rx
= new Regex(@"((d{3,4})|d{3,4}-)?d{7,8}(-d{3})*");
           
return rx.IsMatch(str);
        }


       
/// <summary>
       
/// 检验字符串是否为汉字
       
/// </summary>
       
/// <param name="str">需要检验的字符串</param>
       
/// <returns>是否为汉字:true代表是,false代表否</returns>

        public static bool IsChinese(string str)
       
{
            Regex rx
= new Regex(@"u4e00-u9fa5");
           
return rx.IsMatch(str);
        }


       
/// <summary>
       
/// 检验字符串是否为双字节字符(包括汉字)
       
/// </summary>
       
/// <param name="str">需要检验的字符串</param>
       
/// <returns>是否为双字节字符:true代表是,false代表否</returns>

        public static bool IsDoubleByteChar(string str)
       
{
            Regex rx
= new Regex(@"[^x00-xff]");
           
return rx.IsMatch(str);
        }


       
/// <summary>
       
/// 检验字符串是否为URL地址
       
/// </summary>
       
/// <param name="str">需要检验的字符串</param>
       
/// <returns>是否为URL地址:true代表是,false代表否</returns>

        public static bool IsURLAddress(string str)
       
{
            Regex rx
= new Regex(@"[a-zA-z]+://[^s]*");
           
return rx.IsMatch(str);
        }


       
/// <summary>
       
/// 检验字符串是否为IP地址
       
/// </summary>
       
/// <param name="str">需要检验的字符串</param>
       
/// <returns>是否为IP地址:true代表是,false代表否</returns>

        public static bool IsIPAddress(string str)
       
{
            Regex rx
= new Regex(@"d+.d+.d+.d+");
           
return rx.IsMatch(str);
        }


       
/// <summary>
       
/// 清除字符串中的HTML标签(对于复杂的嵌套标签有时不准确)
       
/// </summary>
       
/// <param name="toEvaluate">指定的要被处理的字符串</param>
       
/// <returns>清除HTML标签后的字符串</returns>

        public static string RemoveHtmlTags(string toEvaluate)
       
{
            Regex rx
= new Regex(@"s/<[a-zA-Z/][^>]*>//g", RegexOptions.IgnoreCase);

           
return rx.Replace(toEvaluate, "");
        }


       
#endregion