C#中如何應用正則表達式判別字符。本站提示廣大學習愛好者:(C#中如何應用正則表達式判別字符)文章只能為提供參考,不一定能成為您想要的結果。以下是C#中如何應用正則表達式判別字符正文
廢話不多說了,上面代碼給大家引見下應用正則表達式判別字符的辦法,詳細代碼如下所示:
using System;
using System.Text.RegularExpressions;
using System.NET;
namespace 正則表達式檢測字符串
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("請輸出字符串:");
string s = Console.ReadLine();
if (GF_IsOk.IsExistHanZi(s))
{
Console.Write("包括漢字");
}
else
{
Console.Write("不包括漢字");
}
Console.ReadLine();
}
}
//判別局部
public class GF_IsOk
{
/// <summary>
/// 判讀能否是IP地址
/// </summary>
/// <param name="in_str"></param>
/// <returns></returns>
public static bool IsIPStr(string in_str)
{
IPAddress ip;
return IPAddress.TryParse(in_str, out ip);
}
/// <summary>
/// 判別能否是數字
/// </summary>
/// <param name="strNumber"></param>
/// <returns></returns>
public static bool IsNumber(string strNumber)
{
Regex objNotNumberPattern = new Regex("[^0-9.-]");
Regex objTwoDotPattern = new Regex("[0-9]*[.][0-9]*[.][0-9]*");
Regex objTwoMinusPattern = new Regex("[0-9]*[-][0-9]*[-][0-9]*");
String strValidRealPattern = "^([-]|[.]|[-.]|[0-9])[0-9]*[.]*[0-9]+$";
String strValidIntegerPattern = "^([-]|[0-9])[0-9]*$";
Regex objNumberPattern = new Regex("(" + strValidRealPattern + ")|(" + strValidIntegerPattern + ")");
return !objNotNumberPattern.IsMatch(strNumber) &&
!objTwoDotPattern.IsMatch(strNumber) &&
!objTwoMinusPattern.IsMatch(strNumber) &&
objNumberPattern.IsMatch(strNumber);
}
/// <summary>
/// 判別能否是日期字符串
/// </summary>
/// <param name="in_str"></param>
/// <returns></returns>
public static bool IsDateStr_yyyymmdd(string in_str)
{
if (in_str == "") return true;
if (in_str.Length != 8) return false;
return IsDateStr(in_str);
}
/// <summary>
/// 判別能否是日期字符串
/// </summary>
/// <param name="in_str"></param>
/// <returns></returns>
public static bool IsDateStr(string in_str)
{
if (in_str == "") return true;
if (in_str.Length == 8)
in_str = in_str.Substring(0, 4) + "-" + in_str.Substring(4, 2) + "-" + in_str.Substring(6, 2);
DateTime dtDate;
bool bValid = true;
try
{
dtDate = DateTime.Parse(in_str);
}
catch (FormatException)
{
// 假如解析辦法失敗則表示不是日期性數據
bValid = false;
}
return bValid;
}
/// <summary>
/// 判別字符串中能否包括漢字,有前往true 否則為false
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static bool IsExistHanZi(string str)
{
Regex reg = new Regex(@"[\u4e00-\u9fa5]");//正則表達式
if (reg.IsMatch(str))
{
return true;
}
else
{
return false;
}
}
/// <summary>
/// 字段串能否為Null或為""(空)
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static bool IsStrNullOrEmpty(string str)
{
if (str == null || str.Trim() == string.Empty)
return true;
return false;
}
/// <summary>
/// 前往文件能否存在
/// </summary>
/// <param name="filename">文件名</param>
/// <returns>能否存在</returns>
public static bool IsFileExists(string filename)
{
return System.IO.File.Exists(filename);
}
/// <summary>
/// 檢測能否契合email格式
/// </summary>
/// <param name="strEmail">要判別的email字符串</param>
/// <returns>判別後果</returns>
public static bool IsValidEmail(string strEmail)
{
return Regex.IsMatch(strEmail, @"^[\w\.]+([-]\w+)*@[A-Za-z0-9-_]+[\.][A-Za-z0-9-_]");
}
public static bool IsValidDoEmail(string strEmail)
{
return Regex.IsMatch(strEmail, @"^@((
[0−9]1,3\.[0−9]1,3\.[0−9]1,3\.)|(([\w−]+\.)+))([a−zA−Z]2,4|[0−9]1,3)(
?)$");
}
/// <summary>
/// 檢測能否是正確的Url
/// </summary>
/// <param name="strUrl">要驗證的Url</param>
/// <returns>判別後果</returns>
public static bool IsURL(string strUrl)
{
return Regex.IsMatch(strUrl, @"^(http|https)\://([a-zA-Z0-9\.\-]+(\:[a-zA-Z0-9\.&%\$\-]+)*@)*((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|localhost|([a-zA-Z0-9\-]+\.)*[a-zA-Z0-9\-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{1,10}))(\:[0-9]+)*(/($|[a-zA-Z0-9\.\,\?\'\\\+&%\$#\=~_\-]+))*$");
}
/// <summary>
/// 判別能否為base64字符串
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static bool IsBase64String(string str)
{
//A-Z, a-z, 0-9, +, /, =
return Regex.IsMatch(str, @"[A-Za-z0-9\+\/\=]");
}
/// <summary>
/// 檢測能否有Sql風險字符
/// </summary>
/// <param name="str">要判別字符串</param>
/// <returns>判別後果</returns>
public static bool IsSafeSqlString(string str)
{
return !Regex.IsMatch(str, @"[-|;|,|\/|||
|
|\}|\{|%|@|\*|!|\']");
}
}
}
以上所述是給大家引見的C#中如何應用正則表達式判別字符,希望對大家有所協助,假如大家有任何疑問請給我留言,會及時回復大家的。在此也十分感激大家對網站的支持!