【发布时间】:2019-02-18 01:57:52
【问题描述】:
我已经浏览了很多关于 SO 的帖子。我没有找到我的特定场景所需的东西。
我需要一个用于字母数字字符串的正则表达式。 满足以下条件的地方
有效字符串:
ameya123 (alphabets and numbers)
ameya (only alphabets)
AMeya12(Capital and normal alphabets and numbers)
Ameya_123 (alphabets and underscore and numbers)
Ameya_ 123 (alphabets underscore and white speces)
无效的字符串:
123 (only numbers)
_ (only underscore)
(only space) (only white spaces)
any special charecter other than underscore
到目前为止我尝试了什么:
(?=.*[a-zA-Z])(?=.*[0-9]*[\s]*[_]*)
上述正则表达式在Regex online editor 中有效,但在c# 中的数据注释中无效
请提出建议。
【问题讨论】:
-
试试
^(?!(?:\d+|_+| +)?$)[\w ]+$。在此处查看现场演示 regex101.com/r/KE96M7/1 -
^(?!\d+$)(?![_\s]+$)[A-Za-z0-9\s_]+$可能也可以。验证是在服务器还是客户端或两者上执行? -
@revo :谢谢..这对我有用..请将其作为答案移植,以便我接受并解释其工作原理:)
-
@AmeyaDeshpande 请参阅my answer,了解一些说明和替代方案。
标签: c# regex asp.net-mvc data-annotations