Prueba esto:
foundMatch = Regex.IsMatch(subjectString, @"^(?=.*[a-z])\w{7,15}\s*$", RegexOptions.IgnoreCase);
Es también permite el uso de _
ya que permitió esto en su intento.
Así que, básicamente, utilizo tres reglas. Uno para verificar si existe al menos una letra. Otro para comprobar si la cadena consta solo de alphas más el _
y finalmente acepto espacios al final y al menos 7 con un máximo de 15 alfa. Estás en una buena pista. Sigue así y se le responder a las preguntas que aquí también :)
Desglose:
"
^ # Assert position at the beginning of the string
(?= # Assert that the regex below can be matched, starting at this position (positive lookahead)
. # Match any single character that is not a line break character
* # Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
[a-z] # Match a single character in the range between “a” and “z”
)
\w # Match a single character that is a “word character” (letters, digits, etc.)
{7,15} # Between 7 and 15 times, as many times as possible, giving back as needed (greedy)
\s # Match a single character that is a “whitespace character” (spaces, tabs, line breaks, etc.)
* # Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
$ # Assert position at the end of the string (or before the line break at the end of the string, if any)
"
¿Por qué debes utilizar una sola expresión regular para esto? A menudo, unas pocas líneas de código claro son mejores que una sola expresión regular contorted. Permitir espacios al final parece extraño; ¿son en realidad parte del nombre de usuario o se ignoran? ¿De verdad quieres tratar a "fred" y "fred" como distintos? ¿Los espacios finales cuentan para la longitud máxima de 7-15? ¿Debe un nombre de usuario ser * al menos * 7 caracteres, o existe una longitud máxima variable que puede estar entre 7 y 15 (y en función de qué)? ¿Qué son exactamente los "personajes especiales"? ¿Es "123_456" válido (es decir, ¿el subrayado cuenta como una letra para los propósitos de la regla 2)? –