Here are my requir开发者_开发技巧ements:
- 1-80 characters
- These characters are allowed:
- alphanumeric
- spaces
_ ( ) [ ] ! # , . & * + : ' / -
The regex I have below works, but in particular I'm not sure how to reuse the character class [\w\(\)\.\-\[\]!#,&*+:'\/]
[\w\(\)\.\-\[\]!#,&*+:'\/][\w\s\(\)\.\-\[\]!#,&*+:'\/]{0,79}
Update:
Thanks for all your answers, this one did the trick
^(?!\s)[\w\s().\-!#&]{1,80}$
Does it really need to be 100% regex? Couldn't you just do
[\w\s\(\)\.\-\[\]!#,&*+:'\/]{1,80}
and separately verify that the first character isn't whitespace?
If the first character can't be white space try this:
(?!\s)[-\w\s().[\]\\!#,&*+:'/]{1,80}
. You may want to "bracket" with ^ in the beginning and $ at the end ^(?!\s)[-\w\s().[\]\\!#,&*+:'/]{1,80}$
, to have the regex match the whole string.
Inside a character class, only ]
and \
need escapes. Even -
doesn't need an escape if it's the first character of the class!
Here's the simplest regex I could reduce it to:
[- \w().[\]!#,&*+:'/]{1,80}
You could use a negative look ahead to check the first character is not white space. Then there is no need to reuse a character class.
(?!\s)[\w\s\(\)\.\-\[\]!#,&*+:'\/]{1,80}
精彩评论