不许喷粪!!!!!!!!!!!!!!!!
[C#] 纯文本查看 复制代码 using System;
using System.Linq;
using System.Text.RegularExpressions;
namespace OIFrameWork.Common
{
public class CheckString
{
#region "调用方法入口"
private static CheckString _instance;
private static readonly object CheckStringLock = new object();
public static CheckString GetInstance()
{
if (_instance != null) return _instance;
lock (CheckStringLock)
{
return _instance ?? ( _instance = new CheckString() );
}
}
#endregion
#region "方法体"
/// <summary>
/// 判断是否文件类型在允许的文件类型之中
/// </summary>
/// <param name="fileType">需要匹配的文件类型</param>
/// <param name="types">系统允许的文件类型</param>
/// <returns>反悔是否匹配</returns>
public bool IsCorrectFileType(String fileType, String[] types)
{
return types.Any(t => fileType == t.Trim().ToLower());
}
/// <summary>
/// 判断输入的字符串只包含汉字
/// </summary>
/// <param name="input">需要检查的字符串</param>
/// <returns>是否只包含汉子</returns>
public bool IsChineseCh(string input)
{
var regex = new Regex("^[\\一-\\值]+$");
return regex.IsMatch(input);
}
/// <summary>
/// 匹配3位或4位区号的电话号码,其中区号可以用小括号括起来,
/// 也可以不用,区号与本地号间可以用连字号或空格间隔,
/// 也可以没有间隔
/// \(0\d{2}\)[- ]?\d{8}|0\d{2}[- ]?\d{8}|\(0\d{3}\)[- ]?\d{7}|0\d{3}[- ]?\d{7}
/// </summary>
/// <param name="input">需要检查的字符串</param>
/// <returns>返回是否与电话号码格式匹配</returns>
public bool IsPhone(string input)
{
var regex = new Regex("^\\(0\\d{2}\\)[- ]?\\d{8}$|^0\\d{2}[- ]?\\d{8}$|^\\(0\\d{3}\\)[- ]?\\d{7}$|^0\\d{3}[- ]?\\d{7}$");
return regex.IsMatch(input);
}
/// <summary>
/// 判断输入的字符串是否是一个合法的手机号
/// </summary>
/// <param name="input">需要检查的手机号码</param>
/// <returns>返回是否匹配手机号码</returns>
public bool IsMobilePhone(string input)
{
var regex = new Regex("^13\\d{9}$");
return regex.IsMatch(input);
}
/// <summary>
/// 判断输入的字符串只包含数字
/// 可以匹配整数和浮点数
/// ^-?\d+$|^(-?\d+)(\.\d+)?$
/// </summary>
/// <param name="input">需要检查的字符串</param>
/// <returns>返回是否匹配只包含数字</returns>
public bool IsNumber(string input)
{
var regex = new Regex("^-?\\d+$|^(-?\\d+)(\\.\\d+)?$");
return regex.IsMatch(input);
}
/// <summary>
/// 匹配非负整数
/// </summary>
/// <param name="input">需要检查的字符串</param>
/// <returns>返回是否匹配非负整数</returns>
public bool IsNotNagtive(string input)
{
var regex = new Regex(@"^\d+$");
return regex.IsMatch(input);
}
/// <summary>
/// 匹配正整数
/// </summary>
/// <param name="input">需要检查的字符串</param>
/// <returns>返回是否匹配正整数</returns>
public bool IsUint(string input)
{
var regex = new Regex("^[0-9]*[1-9][0-9]*$");
return regex.IsMatch(input);
}
/// <summary>
/// 判断输入的字符串字包含英文字母
/// </summary>
/// <param name="input">需要检查的字符串</param>
/// <returns>返回是否匹配包含英文字母</returns>
public bool IsEnglisCh(string input)
{
var regex = new Regex("^[A-Za-z]+$");
return regex.IsMatch(input);
}
/// <summary>
/// 判断输入的字符串是否是一个合法的Email地址
/// </summary>
/// <param name="input">需要检查的字符串</param>
/// <returns>返回是否Email符合匹配的规则</returns>
public bool IsEmail(string input)
{
var regex = new Regex(@"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");
return regex.IsMatch(input);
}
/// <summary>
/// 判断输入的字符串是否只包含数字和英文字母
/// </summary>
/// <param name="input">需要检查的字符串</param>
/// <returns>返回是否与指定匹配相符</returns>
public bool IsNumAndEnCh(string input)
{
var regex = new Regex(@"^[A-Za-z0-9]+$");
return regex.IsMatch(input);
}
/// <summary>
/// 判断输入的字符串是否是一个超链接
/// </summary>
/// <param name="input">需要检查的字符串</param>
/// <returns>返回是否是一个超级连接</returns>
public bool IsUrl(string input)
{
var regex = new Regex(@"^[a-zA-Z]+://(\w+(-\w+)*)(\.(\w+(-\w+)*))*(\?\S*)?$");
return regex.IsMatch(input);
}
/// <summary>
/// 判断输入的字符串是否是表示一个IP地址
/// </summary>
/// <param name="input">被比较的字符串</param>
/// <returns>是IP地址则为Tr</returns>
public bool IsIPv4(string input)
{
var ps = input.Split('.');
var regex = new Regex(@"^\d+$");
foreach (var t in ps)
{
if (!regex.IsMatch(t))
{
return false;
}
if (Convert.ToUInt16(t) > 255)
{
return false;
}
}
return true;
}
/// <summary>
/// 判断输入的字符串是否是合法的IPV6 地址
/// </summary>
/// <param name="input">需要检查的字符串</param>
/// <returns>是否是IPV6地址</returns>
public bool IsIpv6(string input)
{
string pattern;
var temp = input;
var strs = temp.Split(':');
if (strs.Length > 8)
{
return false;
}
var count = StringFunction.GetInstance().GetStringCount(input, "::");
if (count > 1)
{
return false;
}
if (count == 0)
{
pattern = @"^([\da-f]{1,4}:){7}[\da-f]{1,4}$";
var regex = new Regex(pattern);
return regex.IsMatch(input);
}
pattern = @"^([\da-f]{1,4}:){0,5}::([\da-f]{1,4}:){0,5}[\da-f]{1,4}$";
var regex1 = new Regex(pattern);
return regex1.IsMatch(input);
}
/// <summary>
/// 调用Regex中IsMatch函数实现一般的正则表达式匹配
/// </summary>
/// <param name="pattern">要匹配的正则表达式模式。</param>
/// <param name="input">要搜索匹配项的字符串</param>
/// <returns>如果正则表达式找到匹配项,则为 tr;否则,为 false。</returns>
public bool IsMatch(string pattern, string input)
{
var regex = new Regex(pattern);
return regex.IsMatch(input);
}
/// <summary>
/// 验证是否是安全的字符串
/// </summary>
/// <param name="inputString"></param>
/// <returns></returns>
public bool IsSafeSqlString(string inputString)
{
const string sqlStr = @"and|or|exec|execute|insert|select|delete|update|alter|create|drop|count|\*|chr|char|asc|mid|substring|master|truncate|declare|xp_cmdshell|restore|backup|net +user|net +localgroup +administrators";
try
{
if (!string.IsNullOrEmpty(inputString))
{
var strRegex = String.Format(@"\b({0})\b", sqlStr);
var regex = new Regex(strRegex, RegexOptions.IgnoreCase);
if (regex.IsMatch(inputString))
return false;
if (Regex.IsMatch(inputString, @"[-|;|,|\/|\(|\)|\[|\]|\}|\{|%|@|\*|!|\']"))
return false;
}
}
catch
{
return false;
}
return true;
}
/// <summary>
/// 过滤字符串
/// </summary>
/// <param name="sSql">需要过滤得字符串 </param>
/// <returns>返回过滤后的字符串</returns>
public string FilterSql(string sSql)
{
const string sqlStr = @"and|or|exec|execute|insert|select|delete|update|alter|create|drop|count|\*|chr|char|asc|mid|substring|master|truncate|declare|xp_cmdshell|restore|backup|net +user|net +localgroup +administrators";
sSql = sSql.ToLower();
return sqlStr.Split('|').Aggregate(sSql, (current, s) => current.Replace(s, ""));
}
#endregion
}
}
|