[英]regex to allow only alphabets and no special characters, with no space at the beginning
I have tried this: but this isn't working as expected.我试过这个:但这没有按预期工作。 However it restricts user to enter space at the beginning.
但是它限制用户在开始时输入空格。 where did I made the mistake?
我在哪里犯了错误?
Regex that I have tried to build so far: [^-\\s][a-zA-Z\\s]*$
到目前为止我尝试构建的正则表达式:
[^-\\s][a-zA-Z\\s]*$
Should Match: priya, Abc Xyz应该匹配:priya, Abc Xyz
Should not match: <spaces>binayak
, $&&ay%%aac不应该匹配:
<spaces>binayak
, $&&ay%%aac
First of all, you missed the beginning of string anchor ^
.首先,您错过了字符串锚点
^
的开头。
Secondly, the inverse character class [^-\\s]
is a good attempt at not allowing spaces, but at the same time it does allow other special characters to be at the beginning of the string, which we do not want.其次,反字符类
[^-\\s]
是不允许空格的一个很好的尝试,但同时它确实允许其他特殊字符位于字符串的开头,这是我们不想要的。 Instead, we could just something very similar to the second character class, just without the \\s
: [a-zA-Z]
.相反,我们可以只使用与第二个字符类非常相似的东西,只是没有
\\s
: [a-zA-Z]
。
Full regex:完整的正则表达式:
^[a-zA-Z][a-zA-Z\s]*$
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.