First, I know this thread is marked as SOLVED already. I'm new to the forum and if it is considered bad etiquette to reply to solved threads then please do let me know! On to the reply.
It would be possible to construct a regular expression to check the conditions (as you requested):
Starts with alphabetical character
Contains only alphanumeric, ".", "-" or "_" characters
Has a maximum length of 15 characters
Contains only one ".", "-" or "_" character
However that pattern might not be particularly understandable at a glance (or even after careful consideration!) when looking through the code. My suggestion would be to break things down into at least two sections.
[*]Have a nice, simple regex to check that the string starts with [a-z], only contains [a-zA-Z0-9.-_] and has a max. length of 15 characters.
[*]Check for only one ".", "-" or "_" character.
That could be achieved by:
if (preg_match('/^[a-z][a-z0-9._-]{0,14}$/Di', $user) &&
preg_match_all('/[._-]/', $user, $matches) <= 1)
{
// ...
}
If you must stick to only one regular expression (perhaps just for educational purposes) then you might use the following if you want to have a stab at deciphering what it's doing!
/^(?=[a-z0-9]*+[._-]?[a-z0-9]*+$)[a-z][a-z0-9._-]{0,14}$/Di