常用正则表达式字符串格式判断
常用的特殊字符串判断如下:
// 判断是否为手机号
private static Regex RegPhone = new Regex("^1[3456789]\\d{9}$");
// 判断是否为整数(带符号)
private static Regex RegNumberSign = new Regex("^[+-]?[0-9]+$");
// 判断是否为浮点数(不带符号)
private static Regex RegDecimal = new Regex("^[0-9]+[.]?[0-9]+$");
// 判断是否为浮点数(带符号)
private static Regex RegDecimalSign = new Regex("^[+-]?[0-9]+[.]?[0-9]+$"); //等价于^[+-]?\d+[.]?\d+$
// 判断是否为邮箱地址
private static Regex RegEmail = new Regex("^[\\w-]+@[\\w-]+\\.(com|net|org|edu|mil|tv|biz|info)$");
// 判断字符串中是否带有中文
private Regex RegChinese = new Regex(@"[\U4e00-\U9fa5]");
// 判断字符串是否为纯英文
private Regex RegEnglish = new Regex(@"^[A-Za-z]+$");
使用方法如下:
bool ret = RegPhone.Match(【字符串】).Success;
封装成类symbol.cs
using System.Text.RegularExpressions;
namespace WindowsFormsApp
{
public static class symbol
{
/// <summary>
/// 判断是否为手机号
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static bool RegPhone(this string s)
{
return new Regex("^1[3456789]\\d{9}$").Match(s).Success;
}
/// <summary>
/// 判断是否为整数(带符号)
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static bool RegNumberSign(this string s)
{
return new Regex("^[+-]?[0-9]+$").Match(s).Success;
}
/// <summary>
/// 判断是否为浮点数(不带符号)
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static bool RegDecimal(this string s)
{
return new Regex("^[0-9]+[.]?[0-9]+$").Match(s).Success;
}
/// <summary>
/// 判断是否为浮点数(带符号)
/// 等价于^[+-]?\d+[.]?\d+$
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static bool RegDecimalSign(this string s)
{
return new Regex("^[+-]?[0-9]+[.]?[0-9]+$").Match(s).Success;
}
/// <summary>
/// 判断是否为邮箱地址
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static bool RegEmail(this string s)
{
return new Regex("^[\\w-]+@[\\w-]+\\.(com|net|org|edu|mil|tv|biz|info)$").Match(s).Success;
}
/// <summary>
/// 剔除字符串中的英文
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static string DelEnglish(this string s)
{
return Regex.Replace(s, "[A-Z]", "", RegexOptions.IgnoreCase);
}
/// <summary>
/// 剔除字符串中的数字
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static string DelNumber(this string s)
{
return Regex.Replace(s, "[0-9]", "", RegexOptions.IgnoreCase);
}
/// <summary>
/// 剔除字符串中的中文
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static string DelChinese(this string s)
{
string retValue = s;
if (Regex.IsMatch(s, @"[\U4e00-\U9fa5]"))
{
retValue = string.Empty;
var strsStrings = s.ToCharArray();
for (int index = 0; index < strsStrings.Length; index++)
{
if (strsStrings[index] >= 0x4e00 && strsStrings[index] <= 0x9fa5)
{
continue;
}
retValue += strsStrings[index];
}
}
return retValue;
}
/// <summary>
/// 判断是否含有英文
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static bool HaveEngSign(this string s)
{
return new Regex("[a-zA-Z]").Match(s).Success;
}
/// <summary>
/// 判断字符串是否为IP
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static bool IsIpaddress(this string input)
{
string pattern = @"^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]).){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$";
return Regex.IsMatch(input, pattern);
}
}
}
调用方法
bool ret = "字符串".RegDecimalSign();