private static Regex RegPhone = new Regex("^[0-9]+[-]?[0-9]+[-]?[0-9]{1}$");
private static Regex RegNumber = new Regex("^[0-9]+{1}$");
private static Regex RegNumberSign = new Regex("^[+-]?[0-9]+{1}$");
private static Regex RegDecimal = new Regex("^[0-9]+[.]?[0-9]+{1}$");
private static Regex RegDecimalSign = new Regex("^[+-]?[0-9]+[.]?[0-9]+{1}$"); //等价于^[+-]?\d+[.]?\d+$
private static Regex RegEmail = new Regex("^[\\w-]+@[\\w-]+\\.(com|net|org|edu|mil|tv|biz|info){1}$");//w 英文字母或数字的字符串,和 [a-zA-Z0-9] 语法一样
private static Regex RegCHZN = new Regex("[\u4e00-\u9fa5]");//是否有中文
一个例子:
#region 中文检测
/// <summary>
/// 检测是否有中文字符
/// </summary>
/// <param name="inputData"></param>
/// <returns></returns>
public static bool IsHasCHZN(string inputData)
{
Match m = RegCHZN.Match(inputData);
return m.Success;
}
#endregion

本文介绍了一系列常用的正则表达式验证方法,包括电话号码、数字、小数、带符号数字、电子邮件地址及中文字符的验证。通过具体示例展示了如何使用这些正则表达式进行数据验证。
2079

被折叠的 条评论
为什么被折叠?



