RegexHelper

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);
    }

}

 

posted @ 2021-02-28 23:24  devs  阅读(125)  评论(0编辑  收藏  举报