正規表達式

摘要:正規表達式



using System.Text.RegularExpressions;


/// <summary>
/// 檢查是否為數字或者字母
/// </summary>
/// <param name="InputString"></param>
/// <returns></returns>
public static bool IsAlphaNumeric(String InputString)
{            
    return (InputString != string.Empty && !Regex.IsMatch(InputString, "[^a-zA-Z0-9]"))
        ? true : false;
}

/// <summary>
/// 檢查是否為正整數包括零
/// </summary>
/// <param name="InputString"></param>
/// <returns></returns>
public static bool IsWholeNumber(String InputString)
{            
    return (InputString != string.Empty && !Regex.IsMatch(InputString, "[^0-9]"))
        ? true : false;
}

/// <summary>
/// 檢查自然數包括實數
/// </summary>
/// <param name="InputString"></param>
/// <returns></returns>
public static bool IsPositiveNumber(String InputString)
{
    Regex objNotPositivePattern = new Regex("[^0-9.]");
    Regex objPositivePattern = new Regex("^[.][0-9]+$|[0-9]*[.]*[0-9]+$");
    Regex objTwoDotPattern = new Regex("[0-9]*[.][0-9]*[.][0-9]*");
    if (InputString == string.Empty)
        return false;
    else
        return !objNotPositivePattern.IsMatch(InputString) &&
        objPositivePattern.IsMatch(InputString) &&
        !objTwoDotPattern.IsMatch(InputString);
}
/// <summary>
/// 檢查是否為合法email
/// </summary>
/// <param name="InputString"></param>
/// <returns></returns>
public static bool IsEmail(string InputString)
{            
    return (InputString != string.Empty && !Regex.IsMatch(InputString, @"^\w+((-\w+)|(\.\w+))*\@\w+((\.|-)\w+)*\.\w+$"))
        ? true : false;
}
/// <summary>
/// 檢查輸入數字是否位數符合
/// </summary>
/// <param name="InputString"></param>
/// <param name="median"></param>
/// <returns></returns>
public static bool IsNumberMedianMatch(string InputString, int median)
{            
    return (InputString != string.Empty && !Regex.IsMatch(InputString, @"^\d{" + median.ToString() + "}$"))
        ? true : false;
}
/// <summary>
/// 檢查輸入數字是否最少為n位數
/// </summary>
/// <param name="InputString"></param>
/// <param name="median">n</param>
/// <returns></returns>
public static bool IsNumberMedianMatchlast(string InputString, int median)
{           
    return (InputString != string.Empty && !Regex.IsMatch(InputString, @"^\d{" + median.ToString() + ",}$"))
        ? true : false;
}
/// <summary>
/// 檢查輸入數字是否在MAX到MIN位數
/// </summary>
/// <param name="InputString"></param>
/// <param name="Max">最大位數</param>
/// <param name="Min">最小位數</param>
/// <returns></returns>
public static bool IsNumberInRange(string InputString, int Max, int Min)
{            
    return (InputString != string.Empty && !Regex.IsMatch(InputString, @"^\d{" + Max.ToString() + "," + Min.ToString() + "}$"))
        ? true : false;
}
/// <summary>
/// 檢查是否為金額 小數固定為兩位
/// </summary>
/// <param name="InputString"></param>
/// <returns></returns>
public static bool IsAmount(string InputString)
{            
    return (InputString != string.Empty && !Regex.IsMatch(InputString, "^[0-9]+(.[0-9]{2})?$"))
        ? true : false;
}
/// <summary>
/// 檢查是否為金額 小數為一位或兩位
/// </summary>
/// <param name="InputString"></param>
/// <returns></returns>
public static bool IsAmountLast(string InputString)
{            
    return (InputString != string.Empty && !Regex.IsMatch(InputString, "^[0-9]+(.[0-9]{1,2})?$"))
        ? true : false;
}
/// <summary>
/// 檢查是否為非零的正整數
/// </summary>
/// <param name="InputString"></param>
/// <returns></returns>
public static bool IsPositiveInteger(string InputString)
{            
    return (InputString != string.Empty && !Regex.IsMatch(InputString, @"^\+?[1-9][0-9]*$"))
        ? true : false;
}
/// <summary>
/// 檢查是否為非零的負整數
/// </summary>
/// <param name="InputString"></param>
/// <returns></returns>
public static bool IsNegativeInteger(string InputString)
{            
    return (InputString != string.Empty && !Regex.IsMatch(InputString, @"^\-?[1-9][0-9]*$"))
        ? true : false;
}
/// <summary>
/// 檢查字串是否符合長度
/// </summary>
/// <param name="InputString"></param>
/// <param name="median"></param>
/// <returns></returns>
public static bool IsStringMatchLength(string InputString, int median)
{            
    return (InputString != string.Empty && !Regex.IsMatch(InputString, "^.{" + median.ToString() + "}$"))
        ? true : false;
}
/// <summary>
/// 是否為符合型式的密碼,字母開頭,長度6~18,字母及數字和下底線混合
/// </summary>
/// <param name="InputString"></param>
/// <returns></returns>
public static bool IsValidPassword(string InputString)
{            
    return (InputString != string.Empty && !Regex.IsMatch(InputString, @"^[a-zA-Z]\w{5,17}$"))
        ? true : false;
}
/// <summary>
/// 檢查是否為網址
/// </summary>
/// <param name="InputString"></param>
/// <returns></returns>
public static bool IsWebSite(string InputString)
{            
    return (InputString != string.Empty && !Regex.IsMatch(InputString, @"^http://([\w-]+\.)+[\w-]+(/[\w-./?%&=]*)?$"))
        ? true : false;
}
/// <summary>
/// 檢查是否為月份 01~09 , 1~12
/// </summary>
/// <param name="InputString"></param>
/// <returns></returns>
public static bool IsMonth(string InputString)
{            
    return (InputString != string.Empty && !Regex.IsMatch(InputString, "^(0?[1-9]|1[0-2])$"))
        ? true : false;
}
/// <summary>
/// 檢查是否為日期 01~09 , 1~31
/// </summary>
/// <param name="InputString"></param>
/// <returns></returns>
public static bool IsDay(string InputString)
{            
    return (InputString != string.Empty && !Regex.IsMatch(InputString, "^((0?[1-9])|((1|2)[0-9])|30|31)$"))
        ? true : false;
}

/// <summary>
/// 檢查是否有下列符號 ^%&’,;=?$\"
/// </summary>
/// <param name="InputString"></param>
/// <returns></returns>
public static bool IsDelimiter(string InputString)
{            
    return (InputString != string.Empty && !Regex.IsMatch(InputString, "[^%&’,;=?$\x22]+"))
        ? true : false;
}

/// <summary>
/// 檢查是否有逗點分割的字串
/// </summary>
/// <param name="InputString"></param>
/// <returns></returns>
public static bool IsDotString(string InputString)
{            
    return (InputString != string.Empty && !Regex.IsMatch(InputString, @"^([\w\d\u4e00-\u9fa5],?)+$"))
        ? true : false;
}
/// <summary>
/// 檢查是否為漢字
/// </summary>
/// <param name="InputString"></param>
/// <returns></returns>
public static bool IsChiWord(string InputString)
{            
    return (InputString != string.Empty && !Regex.IsMatch(InputString, "^[\u4e00-\u9fa5]{0,}$"))
        ? true : false;
}
/// <summary>
/// 檢查是否為手機號碼.
/// </summary>
/// <param name="InputString">The input email.</param>
/// <returns>
///     <c>true</c> if [is cell phone] [the specified input email]; otherwise, <c>false</c>.
/// </returns>
public static bool IsCellPhone(string InputString)
{            
    return (InputString != string.Empty && !Regex.IsMatch(InputString, "^[0-9]{4}-[0-9]{6}$"))
        ? true : false;
}

/// <summary>
/// 檢查IP位址1.
/// </summary>
/// <param name="strIP">The STR IP.</param>
/// <returns>
///     <c>true</c> if [is correnct IP] [the specified STR IP]; otherwise, <c>false</c>.
/// </returns>
public static bool IsCorrenctIP(string strIP)
{
    string strRegExp = @"(\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])";
    if (System.Text.RegularExpressions.Regex.IsMatch(strIP, strRegExp)) return true;
    else return false;
}

/// <summary>
/// 檢查IP位址2.
/// </summary>
/// <param name="strIP">The STR IP.</param>
/// <returns>
///     <c>true</c> if [is valid IP] [the specified STR IP]; otherwise, <c>false</c>.
/// </returns>

public static bool IsValidIP(string strIP)
{
    if (System.Text.RegularExpressions.Regex.IsMatch(strIP, "[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}"))
    {
        string[] ip_ = strIP.Split('.');
        if (ip_.Length == 4 || ip_.Length == 6)
        {
            if (System.Int32.Parse(ip_[0]) < 256 && System.Int32.Parse(ip_[1]) < 256 & System.Int32.Parse(ip_[2]) < 256 & System.Int32.Parse(ip_[3]) < 256) return true;
            else return false;
        }
        else return false;
    }
    else return false;
}