#region 正则表达式字符串
/// <summary>
/// 正则表达式字符串
/// </summary>
public class StrRegex
{
#region 正则表达式替换字符串
/// <summary>
/// 正则表达式替换字符串
/// </summary>
/// <param name="inputString">字符串内容</param>
/// <param name="pattern">替换字符</param>
/// <param name="replaceStr">替换值</param>
/// <returns></returns>
public static string RegexReplace(string inputString, string pattern, string replaceStr)
{
try
{
return Regex.Replace(inputString, pattern, replaceStr);
}
catch (Exception e)
{
return e.Message;
}
}
#endregion
#region 判断字符串是否为正整数
/// <summary>
/// 判断字符串是否为正整数
/// </summary>
/// <param name="objString">要匹配的字符串</param>
/// <returns>返回真假值,true:匹配;false:不匹配</returns>
public static bool IsInt(String objString)
{
Regex myReg = new Regex(@"^\d+$");
return myReg.IsMatch(objString);
}
#endregion
#region 判断输入的字符串是否全是英文(不区分大小写)
/// <summary>
/// 判断输入的字符串是否全是英文(不区分大小写)
/// </summary>
/// <param name="objString">所要匹配的字符串</param>
/// <returns>返回真假值,true:匹配;false:不匹配</returns>
public static bool isEnglishString(String objString)
{
Regex myReg = new Regex(@"^[a-zA-Z]+$");
return myReg.IsMatch(objString);
}
#endregion
/// <summary>
/// 返回字符串中的数字
/// </summary>
/// <param name="objString"></param>
/// <returns></returns>
public static string RunNumber(string objString)
{
return Regex.Match(objString, "[0-9]+").Value.ToString();
}
/// <summary>
/// 返回字符串中左边的字符
/// </summary>
/// <param name="objString"></param>
/// <returns></returns>
public static string RunLeftString(string objString)
{
return Regex.Match(objString, "[%*/+ -.A-Za-z]+").Value.ToString();
}
/// <summary>
/// 返回字符串中右边的字符
/// </summary>
/// <param name="objString"></param>
/// <returns></returns>
public static string RunRightString(string objString)
{
return Regex.Match(objString, "[%*/+ -.A-Za-z]+$").Value.ToString();
}
/// <summary>
/// 返回字符串中的字符
/// </summary>
/// <param name="objString"></param>
/// <returns></returns>
public static string RunString(string objString)
{
return Regex.Match(objString, "[A-Za-z]+").Value.ToString();
}
#region 判断所输入的字符串是否为中文
/// <summary>
/// 判断所输入的字符串是否为中文
/// </summary>
/// <param name="objString">所要匹配的字符串</param>
/// <returns>返回真假值,true:匹配;false:不匹配</returns>
public static bool isChinese(String objString)
{
Regex myReg = new Regex(@"^[\u4e00-\u9fa5]+$");
return myReg.IsMatch(objString);
}
#endregion
#region 判断输入字符串是否为英文及数字(英文不区分大小写)
/// <summary>
/// 判断输入字符串是否为英文及数字(英文不区分大小写)
/// </summary>
/// <param name="objString">所要匹配的字符串</param>
/// <returns>返回真假值,true:匹配;false:不匹配</returns>
public static bool isEngNum(String objString)
{
Regex myReg = new Regex(@"^[*/+-a-zA-Z0-9]+$");
return myReg.IsMatch(objString);
}
#endregion
#region 判断输入字符串是否为英文A-D及数字(英文限制在A-D之间英文不区分大小写)
/// <summary>
/// 判断输入字符串是否为英文A-D及数字(英文限制在A-D之间英文不区分大小写)
/// </summary>
/// <param name="objString">所要匹配的字符串</param>
/// <returns>返回真假值,true:匹配;false:不匹配</returns>
public static bool isEngNumMax(String objString)
{
Regex myReg = new Regex(@"^[a-dA-D0-9]+$");
return myReg.IsMatch(objString);
}
#endregion
#region 判断是否为英文及数字组合
/// <summary>
/// 判断是否为英文及数字组合
/// </summary>
/// <param name="objString"></param>
/// <returns></returns>
public static bool InEngNum(string objString)
{
//Regex myReg = new Regex(@"^(?![0-9]+$)[a-zA-Z0-9]{1,25}$");
//return myReg.IsMatch(objString);"^[a-zA-Z]\w{5,17}$"
return Regex.IsMatch(objString, @"^[*/+-a-zA-Z0-9]{1,20}$");
}
#endregion
#region 判断输入字符串是否为英文,数字,中文(英文不区分大小写)
/// <summary>
/// 判断输入字符串是否为英文,数字,中文(英文不区分大小写)
/// </summary>
/// <param name="objString">所要匹配的字符串</param>
/// <returns>返回真假值,true:匹配;false:不匹配</returns>
public static bool isChineseEngNum(String objString)
{
Regex myReg = new Regex(@"^[\u4e00-\u9fa5a-zA-Z0-9]+$");
return myReg.IsMatch(objString);
}
#endregion
#region 判断输入字符串是否为小数
/// <summary>
/// 判断输入字符串是否为小数
/// </summary>
/// <param name="objString">所要匹配的字符串</param>
/// <returns>返回真假值,true:匹配;false:不匹配</returns>
public static bool isFloat(String objString)
{
Regex myReg = new Regex(@"^[0-9]+[.][0-9]+|[0-9]+$");
return myReg.IsMatch(objString);
}
#endregion
#region 判断日期格式是否有效
/// <summary>
/// 判断日期格式是否有效
/// </summary>
/// <param name="objString"></param>
/// <returns></returns>
public static bool IsDate(String objString)
{
Regex myReg = new Regex(@"\b(?<year>\d{2,4})-(?<month>\d{1,2})-(?<day>\d{1,2})\b");
return myReg.IsMatch(objString);
}
#endregion
#region 判断字符串是否符合此正则表达试
/// <summary>
/// 判断字符串是否符合此正则表达试
/// </summary>
/// <param name="str">所要匹配的字符串</param>
/// <param name="regString">正则字符串(如:^[1-9]{1}$)</param>
/// <returns>返回真假值,true:匹配;false:不匹配</returns>
public static bool IsFitStrings(String str, String regString)
{
Regex objPattern = new Regex(regString);
bool returnValue = objPattern.IsMatch(str);
return returnValue;
}
|