Your way was saying, "if there is not an underscore or a letter somewhere in the string, then it is invalid," so it does not care if there are any invalid characters as long as there is at least one valid character. My way is saying, "if there is any character in the string that is not an underscore or a letter, then it is invalid". (The "" immediately after the opening square bracket indicates negation in that character class.)