I need help writing a regular expression to meet the following requirements:
- start with a letter
- end with a letter or digit
- have as interior characters only letters, digits, underscore and hyphen
The following expression works well with the exception that it allows underscore as the last character. It should only allow a letter or a digit for the last character:
^[A-Za-z][\w-]*\w$
Use a Character Class
You can use a POSIX character class for this. In particular, you could use the alphanumeric class, which is shorthand for
[A-Za-z0-9]. For example: