Regex rex = new Regex(@"[u4E00-u9FFF]"); var result = rex.Match(registReq.UserName); if (result.Success) { mResp.ErrorInfo = "用户名不能是汉字!"; return true; } Regex rex2 = new Regex(@"^w$"); result = rex2.Match(registReq.UserName); if (!result.Success) { mResp.ErrorInfo = "用户名包含非法字符!"; return true; }
[...]是字符限定
^...$ 是匹配整个字符串
w 是字母数字下划线汉字
即整个字符串必须是字母数字下划线