码迷,mamicode.com
首页 > 其他好文 > 详细

RegexHelper

时间:2021-03-02 11:46:45      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:nbsp   mat   ber   rgb   string   return   sys   static   none   

using System;
using System.Text.RegularExpressions;


public partial class RegexEx
{
    /// <summary>
    /// 验证输入字符串是否与模式字符串匹配,匹配返回true
    /// </summary>
    /// <param name="input">输入字符串</param>
    /// <param name="pattern">模式字符串</param>        
    public static bool IsMatch(string input, string pattern)
    {
        return IsMatch(input, pattern, RegexOptions.IgnoreCase);
    }

    /// <summary>
    /// 验证输入字符串是否与模式字符串匹配,匹配返回true
    /// </summary>
    /// <param name="input">输入的字符串</param>
    /// <param name="pattern">模式字符串</param>
    /// <param name="options">筛选条件</param>
    public static bool IsMatch(string input, string pattern, RegexOptions options)
    {
        return System.Text.RegularExpressions.Regex.IsMatch(input, pattern, options);
    }

    public static bool IsDecimal(string str)
    {
        return IsMatch(str, @"^(-?\d+)(\.\d+)?$", RegexOptions.None);
    }

    public static bool IsNumber(string str)
    {
        return IsMatch(str, @"^-?\d+$|^(-?\d+)(\.\d+)?$", RegexOptions.None);
    }

    public static bool IsInteger(string str)
    {
        return IsMatch(str, @"^-?\d+$", RegexOptions.None);
    }

    public static bool IsChinese(string str)
    {
        //@"[\u4e00-\u9fa5]"
        return IsMatch(str, @"^[\u4e00-\u9fa5]{0,}$", RegexOptions.None);
    }
    public static bool IsEngAndNumber(string str)
    {
        return IsMatch(str, @"^[A-Za-z0-9]+$", RegexOptions.None);
    }
    public static bool IsEngLetter(string str)
    {
        return IsMatch(str, @"^[A-Za-z]+$", RegexOptions.None);
    }
    public static bool IsMail(string str)
    {
        return IsMatch(str, @"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$", RegexOptions.None);
    }
    public static bool IsDoamin(string str)
    {
        return IsMatch(str, @"[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(/.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+/.?", RegexOptions.None);
    }
    public static bool IsUrl(string str)
    {
        return IsMatch(str, @"[a-zA-z]+://[^\s]* 或 ^http://([\w-]+\.)+[\w-]+(/[\w-./?%&=]*)?$", RegexOptions.None);
    }
    public static bool IsDateTime(string str)
    {
        System.DateTime dt;
        return System.DateTime.TryParse(str, out dt);
    }
    public static bool IsIPv4(string str)
    {
        return IsMatch(str, @"^(25[0-4]|2[0-4]\d]|[01]?\d{2}|[1-9])\.(25[0-5]|2[0-4]\d]|[01]?\d?\d)\.(25[0-5]|2[0-4]\d]|[01]?\d?\d)\.(25[0-4]|2[0-4]\d]|[01]?\d{2}|[1-9])$", RegexOptions.None);
    }
    public static bool IsIPv6(string str)
    {
        return IsMatch(str, @"^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$", RegexOptions.None);
    }

    public static string GetUrlDomain(string url)
    {
        string p = @"(http|https)://(?<domain>[^(:|/]*)";
        System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(p, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
        System.Text.RegularExpressions.Match m = reg.Match(url);
        return m.Groups["domain"].Value;
    }

    public static string[] Split(string str, string pattern)
    {
        return System.Text.RegularExpressions.Regex.Split(str, Environment.NewLine,
            System.Text.RegularExpressions.RegexOptions.IgnoreCase);
    }

}

 

RegexHelper

标签:nbsp   mat   ber   rgb   string   return   sys   static   none   

原文地址:https://www.cnblogs.com/honk/p/14461223.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!