Reputation: 3977
I have this Regex I modified to allow underscores, hyphen, letters and numbers. I am trying to modify it further so that it has the following properties:
Here's what I have right now:
^[a-zA-Z0-9_-]*$
Upvotes: 3
Views: 6401
Reputation: 149020
Try this:
^[a-zA-Z0-9](?:[a-zA-Z0-9_-]*[a-zA-Z0-9])?$
Or this, which will simply ensure that the string does not start with a hyphen or underscore:
^[a-zA-Z0-9][a-zA-Z0-9_-]*$
Upvotes: 9
Reputation: 20843
^([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9_-]*[a-zA-Z0-9])$
Either one of the three possibilities:
[a-zA-Z0-9]
[a-zA-Z0-9][a-zA-Z0-9]
[a-zA-Z0-9][a-zA-Z0-9_-]*[a-zA-Z0-9]
Upvotes: 2