I want a regular expression to test that a string meets the following rules:开发者_如何转开发
- It must not begin with
.
. - It must not end with
.
. - It must not include special characters like
!@#$%^&*
, but can include.
. - It must not include two dots
.
side by side.
Sample valid input:
na.me
(single dot in middle)
Sample invalid input:
.name
(begins with dot)name.
(ends with dot)na..me
(includes two dots side-by-side)$name
(special character not allowed in any position)name#
(likewise)na#me
(likewise)
I believe this should work:
^(\w+\.?)*\w+$
If not in ECMAScript, then replace the \w
's with [a-zA-Z_0-9]
.
The approach here is that instead of citing what's NOT acceptable, it's easier to cite what's acceptable.
Translation of the expression is:
- Start with one or many letters, followed by zero or one period (.)
- All of which can occur zero or many times
- End with at least one letter
精彩评论