正则表达式匹配用户名的复杂条件

I need to check if string has anything else than what specified below

  • it must begins from letter (either uppercase or lowercase)
  • can have alphabetical characters
  • can have numeric characters
  • can have dashes
  • can have minus sign -
  • can have underscore _
  • can have comas ,
  • can have dots .
  • length from 4 to 35 caracters no more no less

everything else should not be in this string have have

i am stuck on this:

preg_match('/^[\w]{4,35}$/i', $username)
preg_match('/^[\w]{4,35}$/i', $username)

OK lets see what this doesn't work :

Match the beginning of the string, followed by [a-zA-Z0-9_] at least 4 times with a maximum of 35 times. This is quite different from your requirements.

Instead what you should use :

/^[a-zA-Z][-,.\w]{3,34}$/

The case sensitivity i modifier is not needed. Also I don't think this is exactly what you want. Usually you would need to specify a minimum length which you don't. This can match "a" for example (not a good username)

if( !preg_match('/^[a-zA-Z][a-zA-Z0-9_,.-]*$/', $username ))
    echo "failed
";