You need to check the string so that only the [az] is passed. All that does not fall into this list should not be skipped. Also, if the user has entered uppercase characters, they must be transformed into lowercase characters. Does anyone have a simple solution to this problem? The only thing that comes to the head is checking every character through a loop. But even here it is not clear how to write it correctly.

for(int i = 0; i < login.length(); i++){ if(login.charAt(i) //тут хочу .matches влепить, но не работает так. :((( } 

    1 answer 1

    We translate to lower case:

     String lowerInput = inputString.toLowerCase(); 

    Check for valid characters only:

     if (lowerInput.matches("[az]+")) { // делаем что хотим }; 

    The regular expression [az]+ sets the rule that the string must consist of one or more characters (this is specified by the + sign), which are in the range from а to z .

    • But it does not check anything. If I write abc & * in a line ((^ then I’ll quietly skip it, because there are abc characters in the line. And I need to drop all forbidden characters - Kermach
    • one
      A regular expression does not check anything, it only sets the rule. Tests the str.matches() method. If str satisfies the rule, then the result is true ; if not, then false . - Enikeyschik
    • @Kermach, you would try first before stating :) - iksuy
    • one
      Because the characters & * ((^ are not included in the range az, the result of the test will be negative if the checked line contains at least one of these characters. - Enikeyschik