I’m going to create a regexp for matching the following:
The string should should consist of only lower case alpha-numeric characters (a-z) and a single ‘-‘ should be allowed within. For example, ‘trash-bin’ should be OK, however, ‘trash–bin’ or ‘-bin’ should not.
How should the regexp look like? Also if it could be explained, I’d be happy.
This is what if have so far:
/^[0-9a-z]+-+$/
UPDATED TO:
^(?!\A-)+[0-9a-z]+-?$
What I don’t know is how to disallow several – to follow each other, or disallow the string to begin with a -.
This may do the trick. Test it at http://www.regextester.com/ (select the preg dialect, as it is the one used by php’s
preg_*functions)