Reputation: 296
I have a requirement with this list
test - (incorrect) space at the end test - (correct) no space at the end test - (incorrect) space at the beginning 54test/ - (incorrect) / at the end /54test - (incorrect) / at the beginning 54test/one - (correct) 54test one/my - (correct) 54test /my - (incorrect)
Here's my current regex but I can't really expand it because I'm pretty new with regex. and I Just get this at some post in SO
^[a-z0-9](?!.*?[^\na-z0-9]{2}).*?[a-z0-9]$
Upvotes: 1
Views: 753
Reputation: 626738
You may use
^[a-zA-Z0-9]+(?:[_\/ ][a-zA-Z0-9]+)*$
See regex demo and the regex graph:
Details
^
- start of string[a-zA-Z0-9]+
- 1+ alphanumeric chars(?:[_\/ ][a-zA-Z0-9]+)*
- 0 or more repetitions of _
, /
or space and then 1+ alphanumeric chars$
- end of stringUpvotes: 1