Home > Web Front-end > JS Tutorial > How to verify regular expressions in C#

How to verify regular expressions in C#

php中世界最好的语言
Release: 2018-03-29 14:23:55
Original
3365 people have browsed it

这次给大家带来正则如何对C#进行校验,正则对C#进行校验的注意事项有哪些,下面就是实战案例,一起来看一下。

一、简介

很多时候我们都需要用到一些验证的方法,有时候需要用正则表达式校验数据时,往往需要到网上找很久,结果找到的还不是很符合自己想要的。所以我把自己整理的校验帮助类分享处理,在这里分享一下,给自己留了个底,也给各位需要的朋友们做个参考

二、校验类片段

2.1 邮箱验证

#region IsEmail(是否邮箱)
/// <summary>
/// 是否邮箱
/// </summary>
/// <param name="value">邮箱地址</param>
/// <param name="isRestrict">是否按严格模式验证</param>
/// <returns></returns>
public static bool IsEmail(string value, bool isRestrict=false)
{
  if (value.IsEmpty())
  {
    return false;
  }
  string pattern = isRestrict
    ? @"^(?("")("".+?""@)|(([0-9a-zA-Z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-zA-Z])@))(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,6}))$"
    : @"^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$";
  return value.IsMatch(pattern, RegexOptions.IgnoreCase);
}
/// <summary>
/// 是否存在邮箱
/// </summary>
/// <param name="value">值</param>
/// <param name="isRestrict">是否按严格模式验证</param>
/// <returns></returns>
public static bool HasEmail(string value, bool isRestrict = false)
{
  if (value.IsEmpty())
  {
    return false;
  }
  string pattern = isRestrict
    ? @"^(?("")("".+?""@)|(([0-9a-zA-Z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-zA-Z])@))(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,6}))$"
    : @"^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$";
  return value.IsMatch(pattern, RegexOptions.IgnoreCase);
}
Copy after login

2.2 手机号码验证

#region IsPhoneNumber(是否合法的手机号码)
/// <summary>
/// 是否合法的手机号码
/// </summary>
/// <param name="value">手机号码</param>
/// <returns></returns>
public static bool IsPhoneNumber(string value)
{
  if (value.IsEmpty())
  {
    return false;
  }
  return value.IsMatch(@"^(0|86|17951)?(13[0-9]|15[012356789]|18[0-9]|14[57]|17[678])[0-9]{8}$");
}
#endregion
#region IsMobileNumber(是否手机号码)
/// <summary>
/// 是否手机号码
/// </summary>
/// <param name="value">手机号码</param>
/// <param name="isRestrict">是否按严格模式验证</param>
/// <returns></returns>
public static bool IsMobileNumberSimple(string value, bool isRestrict = false)
{
  if (value.IsEmpty())
  {
    return false;
  }
  string pattern=isRestrict ? @"^[1][3-8]\d{9}$" : @"^[1]\d{10}$";
  return value.IsMatch(pattern);
}
/// <summary>
/// 是否手机号码
/// </summary>
/// <param name="value">手机号码</param>
/// <returns></returns>
public static bool IsMobileNumber(string value)
{
  if (value.IsEmpty())
  {
    return false;
  }
  value = value.Trim().Replace("^", "").Replace("$", "");
  /**
   * 手机号码: 
   * 13[0-9], 14[5,7], 15[0, 1, 2, 3, 5, 6, 7, 8, 9], 17[6, 7, 8], 18[0-9], 170[0-9]
   * 移动号段: 134,135,136,137,138,139,150,151,152,157,158,159,182,183,184,187,188,147,178,1705
   * 联通号段: 130,131,132,155,156,185,186,145,176,1709
   * 电信号段: 133,153,180,181,189,177,1700
   */
  return value.IsMatch(@"^1(3[0-9]|4[57]|5[0-35-9]|8[0-9]|70)\d{8}$");
}
/// <summary>
/// 是否存在手机号码
/// </summary>
/// <param name="value">值</param>
/// <param name="isRestrict">是否按严格模式验证</param>
/// <returns></returns>
public static bool HasMobileNumberSimple(string value, bool isRestrict = false)
{
  if (value.IsEmpty())
  {
    return false;
  }
  string pattern = isRestrict ? @"[1][3-8]\d{9}" : @"[1]\d{10}";
  return value.IsMatch(pattern);
}
#endregion
#region IsChinaMobilePhone(是否中国移动号码)
/// <summary>
/// 是否中国移动号码
/// </summary>
/// <param name="value">手机号码</param>
/// <returns></returns>
public static bool IsChinaMobilePhone(string value)
{
  if (value.IsEmpty())
  {
    return false;
  }
  /**
   * 中国移动:China Mobile
   * 134,135,136,137,138,139,150,151,152,157,158,159,182,183,184,187,188,147,178,1705
   */
  return value.IsMatch(@"(^1(3[4-9]|4[7]|5[0-27-9]|7[8]|8[2-478])\d{8}$)|(^1705\d{7}$)");
}
#endregion
#region IsChinaUnicomPhone(是否中国联通号码)
/// <summary>
/// 是否中国联通号码
/// </summary>
/// <param name="value">手机号码</param>
/// <returns></returns>
public static bool IsChinaUnicomPhone(string value)
{
  if (value.IsEmpty())
  {
    return false;
  }
  /**
   * 中国联通:China Unicom
   * 130,131,132,155,156,185,186,145,176,1709
   */
  return value.IsMatch(@"(^1(3[0-2]|4[5]|5[56]|7[6]|8[56])\d{8}$)|(^1709\d{7}$)");
}
#endregion
#region IsChinaTelecomPhone(是否中国电信号码)
/// <summary>
/// 是否中国电信号码
/// </summary>
/// <param name="value">手机号码</param>
/// <returns></returns>
public static bool IsChinaTelecomPhone(string value)
{
  if (value.IsEmpty())
  {
    return false;
  }
  /**
   * 中国电信:China Telecom
   * 133,153,180,181,189,177,1700
   */
  return value.IsMatch(@"(^1(33|53|77|8[019])\d{8}$)|(^1700\d{7}$)");
}
Copy after login

2.3 身份证验证

#region IsIdCard(是否身份证号码)
/// <summary>
/// 是否身份证号码
/// </summary>
/// <param name="value">身份证</param>
/// <returns></returns>
public static bool IsIdCard(string value)
{
  if (value.IsEmpty())
  {
    return false;
  }
  if (value.Length == 15)
  {
    return value.IsMatch(@"^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$");
  }
  return value.Length == 0x12 &&
      value.IsMatch(@"^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[Xx])$",
        RegexOptions.IgnoreCase);
}
Copy after login

2.4 Base64编码验证

#region IsBase64String(是否Base64编码)
/// <summary>
/// 是否Base64编码
/// </summary>
/// <param name="value">Base64字符串</param>
/// <returns></returns>
public static bool IsBase64String(string value)
{
  return value.IsMatch(@"[A-Za-z0-9\+\/\=]");
}
Copy after login

2.5 时间验证

#region IsDate(是否日期)
/// <summary>
/// 是否日期
/// </summary>
/// <param name="value">日期字符串</param>
/// <param name="isRegex">是否正则验证</param>
/// <returns></returns>
public static bool IsDate(string value,bool isRegex=false)
{
  if (value.IsEmpty())
  {
    return false;
  }
  if (isRegex)
  {
    //考虑到4年一度的366天,还有特殊的2月的日期
    return
      value.IsMatch(
        @"^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-)) (20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d$");
  }
  DateTime minValue;
  return DateTime.TryParse(value, out minValue);
}
/// <summary>
/// 是否日期
/// </summary>
/// <param name="value">日期字符串</param>
/// <param name="format">格式化字符串</param>
/// <returns></returns>
public static bool IsDate(string value, string format)
{
  return IsDate(value, format, null, DateTimeStyles.None);
}
/// <summary>
/// 是否日期
/// </summary>
/// <param name="value">日期字符串</param>
/// <param name="format">格式化字符串</param>
/// <param name="provider">格式化提供者</param>
/// <param name="styles">日期格式</param>
/// <returns></returns>
public static bool IsDate(string value, string format, IFormatProvider provider, DateTimeStyles styles)
{
  if (value.IsEmpty())
  {
    return false;
  }
  DateTime minValue;
  return DateTime.TryParseExact(value, format, provider, styles, out minValue);
}
#endregion
#region IsDateTime(是否有效时间)
/// <summary>
/// 是否大于最小时间
/// </summary>
/// <param name="value">时间</param>
/// <param name="min">最小时间</param>
/// <returns></returns>
public static bool IsDateTimeMin(string value, DateTime min)
{
  if (value.IsEmpty())
  {
    return false;
  }
  DateTime dateTime;
  if (DateTime.TryParse(value, out dateTime))
  {
    if (DateTime.Compare(dateTime, min) >= 0)
    {
      return true;
    }
  }
  return false;
}
/// <summary>
/// 是否小于最大时间
/// </summary>
/// <param name="value">时间</param>
/// <param name="max">最大时间</param>
/// <returns></returns>
public static bool IsDateTimeMax(string value, DateTime max)
{
  if (value.IsEmpty())
  {
    return false;
  }
  DateTime dateTime;
  if (DateTime.TryParse(value, out dateTime))
  {
    if (DateTime.Compare(max, dateTime) >= 0)
    {
      return true;
    }
  }
  return false;
}
Copy after login

2.6 Url验证

#region IsUrl(是否Url地址)
/// <summary>
/// 是否Url地址(统一资源定位)
/// </summary>
/// <param name="value">url地址</param>
/// <returns></returns>
public static bool IsUrl(string value)
{
  if (value.IsEmpty())
  {
    return false;
  }
  return
    value.IsMatch(
      @"^(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\.\,\?\'\\\+&%\$#\=~_\-]+))*$",
      RegexOptions.IgnoreCase);
}
#endregion
#region IsUri(是否Uri)
/// <summary>
/// 是否Uri(统一资源标识)
/// </summary>
/// <param name="value">uri</param>
/// <returns></returns>
public static bool IsUri(string value)
{
  if (value.IsEmpty())
  {
    return false;
  }
  if (value.IndexOf(".", StringComparison.OrdinalIgnoreCase) == -1)
  {
    return false;
  }
  var schemes = new[]
  {
    "file",
    "ftp",
    "gopher",
    "http",
    "https",
    "ldap",
    "mailto",
    "net.pipe",
    "net.tcp",
    "news",
    "nntp",
    "telnet",
    "uuid"
  };
  bool hasValidSchema = false;
  foreach (string scheme in schemes)
  {
    if (hasValidSchema)
    {
      continue;
    }
    if (value.StartsWith(scheme, StringComparison.OrdinalIgnoreCase))
    {
      hasValidSchema = true;
    }
  }
  if (!hasValidSchema)
  {
    value = "http://" + value;
  }
  return Uri.IsWellFormedUriString(value, UriKind.Absolute);
}
#endregion
#region IsMainDomain(是否主域名)
/// <summary>
/// 是否主域名或者www开头的域名
/// </summary>
/// <param name="value">url地址</param>
/// <returns></returns>
public static bool IsMainDomain(string value)
{
  if (value.IsEmpty())
  {
    return false;
  }
  return
    value.IsMatch(
      @"^http(s)?\://((www.)?[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\.\,\?\'\\\+&%\$#\=~_\-]+))*$");
}
#endregion
Copy after login

2.7 基础格式验证

#region IsGuid(是否Guid)
/// <summary>
/// 是否Guid
/// </summary>
/// <param name="guid">Guid字符串</param>
/// <returns></returns>
public static bool IsGuid(string guid)
{
  if (guid.IsEmpty())
  {
    return false;
  }
  return guid.IsMatch(@"[A-F0-9]{8}(-[A-F0-9]{4}){3}-[A-F0-9]{12}|[A-F0-9]{32}", RegexOptions.IgnoreCase);
}
#endregion
#region IsPositiveInteger(是否大于0的正整数)
/// <summary>
/// 是否大于0的正整数
/// </summary>
/// <param name="value">正整数</param>
/// <returns></returns>
public static bool IsPositiveInteger(string value)
{
  if (value.IsEmpty())
  {
    return false;
  }
  return value.IsMatch(@"^[1-9]+\d*$");
}
#endregion
#region IsInt32(是否Int32类型)
/// <summary>
/// 是否Int32类型
/// </summary>
/// <param name="value">整数</param>
/// <returns></returns>
public static bool IsInt32(string value)
{
  if (value.IsEmpty())
  {
    return false;
  }
  return value.IsMatch(@"^[0-9]*$");
}
#endregion
#region IsDouble(是否Double类型,如果带有.默认为1位0)
/// <summary>
/// 是否Double类型
/// </summary>
/// <param name="value">小数</param>
/// <returns></returns>
public static bool IsDouble(string value)
{
  if (value.IsEmpty())
  {
    return false;
  }
  return value.IsMatch(@"^\d[.]?\d?$");
}
/// <summary>
/// 是否Double类型
/// </summary>
/// <param name="value">小数</param>
/// <param name="minValue">最小值</param>
/// <param name="maxValue">最大值</param>
/// <param name="digit">小数位数,如果是0则不检测</param>
/// <returns></returns>
public static bool IsDouble(string value, double minValue, double maxValue, int digit)
{
  if (value.IsEmpty())
  {
    return false;
  }
  string patten = string.Format(@"^\d[.]?\d{0}$", "{0,10}");
  if (digit > 0)
  {
    patten = string.Format(@"^\d[.]?\d{0}$", "{" + digit + "}");
  }
  if (value.IsMatch(patten))
  {
    double val = Convert.ToDouble(value);
    if (val >= minValue && val <= maxValue)
    {
      return true;
    }
  }
  return false;
}
#endregion
#region IsInteger(是否整数)
/// <summary>
/// 是否整数
/// </summary>
/// <param name="value">值</param>
/// <returns>结果</returns>
public static bool IsInteger(string value)
{
  if (value.IsEmpty())
  {
    return false;
  }
  return value.IsMatch(@"^\-?[0-9]+$");
}
#endregion
#region IsUnicode(是否Unicode字符串)
/// <summary>
/// 是否Unicode字符串
/// </summary>
/// <param name="value">unicode字符串</param>
/// <returns>结果</returns>
public static bool IsUnicode(string value)
{
  if (value.IsEmpty())
  {
    return false;
  }
  return
    value.IsMatch(
      @"^(http|https|ftp|rtsp|mms):(\/\/|\\\\)[A-Za-z0-9%\-_@]+\.[A-Za-z0-9%\-_@]+[A-Za-z0-9\.\/=\?%\-&_~`@:\+!;]*$");
}
#endregion
#region IsDecimal(是否数字型)
/// <summary>
/// 是否数字型
/// </summary>
/// <param name="value">数字</param>
/// <returns></returns>
public static bool IsDecimal(string value)
{
  if (value.IsEmpty())
  {
    return false;
  }
  return value.IsMatch(@"^([0-9])[0-9]*(\.\w*)?$");
}
#endregion
Copy after login

2.8 IP、Mac地址验证

#region IsMac(是否Mac地址)
/// <summary>
/// 是否Mac地址
/// </summary>
/// <param name="value">Mac地址</param>
/// <returns></returns>
public static bool IsMac(string value)
{
  if (value.IsEmpty())
  {
    return false;
  }
  return value.IsMatch(@"^([0-9A-F]{2}-){5}[0-9A-F]{2}$") || value.IsMatch(@"^[0-9A-F]{12}$");
}
#endregion
#region IsIpAddress(是否IP地址)
/// <summary>
/// 是否IP地址
/// </summary>
/// <param name="value">ip地址</param>
/// <returns>结果</returns>
public static bool IsIpAddress(string value)
{
  if (value.IsEmpty())
  {
    return false;
  }
  return value.IsMatch(@"^(\d(25[0-5]|2[0-4][0-9]|1?[0-9]?[0-9])\d\.){3}\d(25[0-5]|2[0-4][0-9]|1?[0-9]?[0-9])\d$");
}
Copy after login

2.9 字符串验证

#region IsVersion(是否有效的版本号)
/// <summary>
/// 是否有效版本号,范例:1.3,1.1.5,1.25.256
/// </summary>
/// <param name="value">版本号</param>
/// <param name="length">长度</param>
/// <returns></returns>
public static bool IsVersion(string value, int length = 5)
{
  if (value.IsEmpty())
  {
    return false;
  }
  value = value.Replace("^", "").Replace("$", "");
  return value.IsMatch(string.Format(@"^{0}{1}{2}$", @"\d{0,4}\.(\d{1,4}\.){0,", length, @"}\d{1,4}"));
}
#endregion
#region IsContainsChinese(是否包含中文)
/// <summary>
/// 是否中文
/// </summary>
/// <param name="value">中文</param>
/// <returns></returns>
public static bool IsChinese(string value)
{
  if (value.IsEmpty())
  {
    return false;
  }
  return value.IsMatch(@"^[\u4e00-\u9fa5]+$", RegexOptions.IgnoreCase);
}
/// <summary>
/// 是否包含中文
/// </summary>
/// <param name="value">中文</param>
/// <returns></returns>
public static bool IsContainsChinese(string value)
{
  if (value.IsEmpty())
  {
    return false;
  }
  return value.IsMatch(@"[\u4e00-\u9fa5]+",RegexOptions.IgnoreCase);
}
#endregion
#region IsContainsNumber(是否包含数字)
/// <summary>
/// 是否包含数字
/// </summary>
/// <param name="value">数字</param>
/// <returns></returns>
public static bool IsContainsNumber(string value)
{
  if (value.IsEmpty())
  {
    return false;
  }
  return value.IsMatch(@"[0-9]+");
}
#endregion
#region IsLengthStr(字符串长度是否在指定范围内)
/// <summary>
/// 字符串长度是否在指定范围内,一个中文为2个字符
/// </summary>
/// <param name="value">字符串</param>
/// <param name="begin">开始</param>
/// <param name="end">结束</param>
/// <returns></returns>
public static bool IsLengthStr(string value, int begin, int end)
{
  int length = Regex.Replace(value, @"[^\x00-\xff]", "OK").Length;
  if ((length <= begin) && (length >= end))
  {
    return false;
  }
  return true;
}
#endregion
#region IsNormalChar(是否正常字符,字母、数字、下划线的组合)
/// <summary>
/// 是否正常字符,字母、数字、下划线的组合
/// </summary>
/// <param name="value">字符串</param>
/// <returns></returns>
public static bool IsNormalChar(string value)
{
  if (value.IsEmpty())
  {
    return false;
  }
  return value.IsMatch(@"[\w\d_]+", RegexOptions.IgnoreCase);
}
#endregion
#region IsPostfix(是否指定后缀)
/// <summary>
/// 是否指定后缀
/// </summary>
/// <param name="value">字符串</param>
/// <param name="postfixs">后缀名数组</param>
/// <returns></returns>
public static bool IsPostfix(string value, string[] postfixs)
{
  if (value.IsEmpty())
  {
    return false;
  }
  string postfix = string.Join("|", postfixs);
  return value.IsMatch(string.Format(@".(?i:{0})$", postfix));
}
#endregion
#region IsRepeat(是否重复)
/// <summary>
/// 是否重复,范例:112,返回true
/// </summary>
/// <param name="value">值</param>
/// <returns></returns>
public static bool IsRepeat(string value)
{
  if (value.IsEmpty())
  {
    return false;
  }
  var array = value.ToCharArray();
  return array.Any(c => array.Count(t => t == c) > 1);
}
Copy after login

2.10 邮政编码验证

#region IsPostalCode(是否邮政编码)
/// <summary>
/// 是否邮政编码,6位数字
/// </summary>
/// <param name="value">邮政编码</param>
/// <returns></returns>
public static bool IsPostalCode(string value)
{
  if (value.IsEmpty())
  {
    return false;
  }
  return value.IsMatch(@"^[1-9]\d{5}$", RegexOptions.IgnoreCase);
}
Copy after login

2.11 中国固话验证

#region IsTel(是否中国电话)
/// <summary>
/// 是否中国电话,格式:010-85849685
/// </summary>
/// <param name="value">电话</param>
/// <returns></returns>
public static bool IsTel(string value)
{
  if (value.IsEmpty())
  {
    return false;
  }
  return value.IsMatch(@"^\d{3,4}-?\d{6,8}$", RegexOptions.IgnoreCase);
}
Copy after login

2.12 QQ号码验证

#region IsQQ(是否合法QQ号码)
/// <summary>
/// 是否合法QQ号码
/// </summary>
/// <param name="value">QQ号码</param>
/// <returns></returns>
// ReSharper disable once InconsistentNaming
public static bool IsQQ(string value)
{
  if (value.IsEmpty())
  {
    return false;
  }
  return value.IsMatch(@"^[1-9][0-9]{4,9}$");
}
#endregion
Copy after login

三、源代码

最后 附上源码地址

https://github.com/jianxuanbing/JCE/blob/master/JCE.Utils/Valid.cs

四、正则表达式

4.1 校验数字的表达式

数字:^[0-9]*$
n位的数字:^\d{n}$
至少n位的数字:^\d{n,}$
m-n位的数字:^\d{m,n}$
零和非零开头的数字:^(0|[1-9][0-9]*)$
非零开头的最多带两位小数的数字:^([1-9][0-9]*)+(.[0-9]{1,2})?$
带1-2位小数的正数或负数:^(\-)?\d+(\.\d{1,2})?$
正数、负数、和小数:^(\-|\+)?\d+(\.\d+)?$
有两位小数的正实数:^[0-9]+(.[0-9]{2})?$
有1~3位小数的正实数:^[0-9]+(.[0-9]{1,3})?$
非零的正整数:^[1-9]\d*$ 或 ^([1-9][0-9]*){1,3}$ 或 ^\+?[1-9][0-9]*$
非零的负整数:^\-[1-9][]0-9"*$ 或 ^-[1-9]\d*$
非负整数:^\d+$ 或 ^[1-9]\d*|0$
非正整数:^-[1-9]\d*|0$ 或 ^((-\d+)|(0+))$
非负浮点数:^\d+(\.\d+)?$ 或 ^[1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0$
非正浮点数:^((-\d+(\.\d+)?)|(0+(\.0+)?))$ 或 ^(-([1-9]\d*\.\d*|0\.\d*[1-9]\d*))|0?\.0+|0$
正浮点数:^[1-9]\d*\.\d*|0\.\d*[1-9]\d*$ 或 ^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$
负浮点数:^-([1-9]\d*\.\d*|0\.\d*[1-9]\d*)$ 或 ^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$
浮点数:^(-?\d+)(\.\d+)?$ 或 ^-?([1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0)$

4.2 校验字符的表达式

Chinese characters: ^[\u4e00-\u9fa5]{0,}$
English and numbers: ^[A-Za-z0-9]+$ or ^[A-Za-z0-9]{4 ,40}$
All characters with a length of 3-20: ^.{3,20}$
A string consisting of 26 English letters: ^[A-Za-z]+$
A string consisting of 26 uppercase English letters: ^[A-Z]+$
A string consisting of 26 lowercase English letters: ^[a-z]+$
A character consisting of numbers and 26 English letters String: ^[A-Za-z0-9]+$
String consisting of numbers, 26 English letters or underscores: ^\w+$ or ^\w{3,20}$
Chinese, English, numbers including underscores: ^[\u4E00-\u9FA5A-Za-z0-9_]+$
Chinese, English, numbers but excluding underscores and other symbols: ^[\u4E00-\u9FA5A-Za-z0-9 ]+$ or ^[\u4E00-\u9FA5A-Za-z0-9]{2,20}$
You can enter characters containing ^%&',;=?$\": [^%&', ;=?$\x22]+
It is forbidden to enter characters containing ~: [^~\x22]+

4.3 Special requirement expression

Email address: ^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$
Domain name :[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(/.[a-zA-Z0-9][-a-zA-Z0-9]{0, 62})+/.?
InternetURL: [a-zA-z]+://[^\s]* or ^http://([\w-]+\.)+[\w- ]+(/[\w-./?%&=]*)?$
Mobile phone number: ^(13[0-9]|14[5|7]|15[0|1|2|3 |5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$
Phone number("XXX-XXXXXXX" , "XXXX-XXXXXXXX", "XXX-XXXXXXX", "XXX-XXXXXXXX", "XXXXXXX" and "XXXXXXXX): ^(\(\d{3,4}-)|\d{3.4}-)?\ d{7,8}$
Domestic phone number (0511-4405222, 021-87888822):\d{3}-\d{8}|\d{4}-\d{7}
Identity ID number (15 digits, 18 digits): ^\d{15}|\d{18}$
Short ID number (numbers, letters ending in x): ^([0-9]){7, 18}(x|X)?$ or^\d{8,18}|[0-9x]{8,18}|[0-9X]{8,18}?$
Is the account legal (letter Beginning, 5-16 bytes are allowed, alphanumeric underscores are allowed): ^[a-zA-Z][a-zA-Z0-9_]{4,15}$
Password (starts with a letter, length is 6 ~18, can only contain letters, numbers and underscores): ^[a-zA-Z]\w{5,17}$
Strong password (must contain a combination of uppercase and lowercase letters and numbers, # cannot be used ##Special characters, length between 8-10): ^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,10} $ Date format: ^\d{4}-\d{1,2}-\d{1,2}
12 months of the year (01~09 and 1~12): ^( 0?[1-9]|1[0-2])$
31 days of a month (01~09 and 1~31): ^((0?[1-9])|((1| 2)[0-9])|30|31)$
Input format of money:
xml file: ^([a-zA-Z]+-?)+[a-zA-Z0-9 ]+\\.[x|X][m|M][l|L]$
Regular expression for Chinese characters: [\u4e00-\u9fa5]
Double-byte characters: [^\x00 -\xff] (Including Chinese characters, it can be used to calculate the length of the string (the length of a double-byte character is counted as 2, and the ASCII character is counted as 1))
Regular expression for blank lines:\n\s*\ r (can be used to delete blank lines)
Regular expression for HTML tags: <(\S*?)[^>]*>.*?|<.*? /> (The version circulating on the Internet is too bad, the above one is only partially effective, and it is still powerless for complex nested tags)
Regular expression for leading and trailing whitespace characters: ^\s*|\s*$ or (^ \s*)|(\s*$) (can be used to delete whitespace characters at the beginning and end of the line (including spaces, tabs, form feeds, etc.), a very useful expression)
Tencent QQ number :[1-9][0-9]{4,} (Tencent QQ number starts from 10000)
China Postal Code: [1-9]\d{5}(?!\d) (China Postal Code is a 6-digit number)
IP address:\d+\.\d+\.\d+\.\d+ (useful when extracting IP address)
IP address: ((?:(?:25[0-5 ]|2[0-4]\\d|[01]?\\d?\\d)\\.){3}(?:25[0-5]|2[0-4]\\d |[01]?\\d?\\d))

4.4 Money input format

There are four acceptable representations of money: "10000.00" and "10,000.00", and "10000" and "10,000" without "cents": ^[1-9][0-9]*$
This means any number that does not start with 0, but it also means that a character "0" does not pass, so we use the following form: ^(0|[1-9][0-9]*) $
A 0 or a number that does not start with 0. We can also allow a negative sign at the beginning: ^(0|-?[1-9][0-9]*)$
This means a 0 or a number that may be negative and does not start with 0. Let the user start with 0. Also remove the negative sign, because money cannot be negative. What we need to add below is to explain the possible decimal part: ^[0-9]+(.[0-9]+)?$
It must be noted that there should be at least 1 digit after the decimal point, so "10." is not passed, but "10" and "10.2" is passed: ^[0-9]+(.[0-9]{2})?$
In this way, we stipulate that there must be two digits after the decimal point. If you think it is too harsh, you can do this: ^[0-9]+(.[0-9]{1,2})?$
This allows the user to write only one decimal place. Now we should consider the comma in the number. We can do this: ^ [0-9]{1,3}(,[0-9]{3})*(.[0-9]{1,2})?$
1 to 3 numbers, followed by any number Comma + 3 digits, comma becomes optional instead of required: ^([0-9]+|[0-9]{1,3}(,[0-9]{3})*)(.[ 0-9]{1,2})?$
Note: This is the final result, don’t forget that "+" can be replaced with "*" if you think the empty string is acceptable (strange, why? ) Finally, don’t forget to remove the backslash when using the function. Common mistakes are here

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related topics on the PHP Chinese website article!

Recommended reading:

15 frequently used regular expressions for front-end form validation

How to use regular expressions Verify the registry

The above is the detailed content of How to verify regular expressions in C#. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template