开发者

A regular expression about folder name

I want a regular expression to test that a string meets the following rules:开发者_如何转开发

  1. It must not begin with ..
  2. It must not end with ..
  3. It must not include special characters like !@#$%^&*, but can include ..
  4. 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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜