• 技术文章 >后端开发 >C#.Net教程

    分享多个C#常用正则表达式的实例

    Y2JY2J2017-04-26 13:43:16原创977
    using System;  
    using System.Text.RegularExpressions;  
    namespace CommonTools  
    {  
    /**//// <summary>  
    /// RegexLib 的摘要说明。  
    /// </summary>  
    public class RegexLib  
    {  
    //验证Email地址  
    public static bool IsValidEmail(string strIn)  
    {  
    // Return true if strIn is in valid e-mail format.  
    return Regex.IsMatch(strIn, @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");  
    }  
    //dd-mm-yy 的日期形式代替 mm/dd/yy 的日期形式。  
    public static string MDYToDMY(String input)  
    {  
    return Regex.Replace(input,"\\b(?\\d{1,2})/(?\\d{1,2})/(?\\d{2,4})\\b","${day}-${month}-${year}");  
    }  
    //验证是否为小数  
    public static bool IsValidDecimal(string strIn)  
    {  
    return Regex.IsMatch(strIn,@"[0].\d{1,2}|[1]");  
    }  
    //验证是否为电话号码  
    public static bool IsValidTel(string strIn)  
    {  
    return Regex.IsMatch(strIn,@"(\d+-)?(\d{4}-?\d{7}|\d{3}-?\d{8}|^\d{7,8})(-\d+)?");  
    }  
    //验证年月日  
    public static bool IsValidDate(string strIn)  
    {  
    return Regex.IsMatch(strIn,@"^2\d{3}-(?:0?[1-9]|1[0-2])-(?:0?[1-9]|[1-2]\d|3[0-1])(?:0?[1-9]|1\d|2[0-3]):(?:0?[1-9]|[1-5]\d):(?:0?[1-9]|[1-5]\d)$");  
    }  
    //验证后缀名  
    public static bool IsValidPostfix(string strIn)  
    {  
    return Regex.IsMatch(strIn,@"\.(?i:gif|jpg)$");  
    }  
    //验证字符是否再4至12之间  
    public static bool IsValidByte(string strIn)  
    {  
    return Regex.IsMatch(strIn,@"^[a-z]{4,12}$");  
    }  
    //验证IP  
    public static bool IsValidIp(string strIn)  
    {  
    return Regex.IsMatch(strIn,@"^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$");  
    }  
    }  
    }

    php入门到就业线上直播课:查看学习

    以上就是分享多个C#常用正则表达式的实例的详细内容,更多请关注php中文网其它相关文章!

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。

    前端(VUE)零基础到就业课程:点击学习

    清晰的学习路线+老师随时辅导答疑

    自己动手写 PHP MVC 框架:点击学习

    快速了解MVC架构、了解框架底层运行原理

    专题推荐:C#,正则表达式
    上一篇:页面包含处理实例详解 下一篇:自己动手写 PHP MVC 框架(40节精讲/巨细/新人进阶必看)

    相关文章推荐

    • ❤️‍🔥共22门课程,总价3725元,会员免费学• ❤️‍🔥接口自动化测试不想写代码?• c语言中关键字有多少个• 解决asp.net中“从客户端中检测到有潜在危险的Request.Form值”的错误• asp.net 图片验证码的HtmlHelper• ASP.NET使用Ajax如何返回Json对象的方法具体介绍• 应用绝对路径与相对路径
    1/1

    PHP中文网