Answer the question
In order to leave comments, you need to log in
How can I change the code so that it passes the regular expression test?
Implement the removal of extra characters when entering a phone number in the console and checking that the number matches the Russian mobile number format. If the entered string cannot be converted to the format of a mobile number, display a message about incorrect input. The phone can be entered not only in the format 79091234567, but also with extra characters.
The task involves the use of regular expressions. The code passes all required tests, but the regular expression test fails.
public class PhoneCleanerRegex {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (true) {
String input = scanner.nextLine();
if (input.equals("0")) {
break;
}
//TODO:напишите ваш код тут, результат вывести в консоль.
input = input.replaceAll("[^0-9]", "");
if (Pattern.matches("^7[0-9]{10}$", input)) {
System.out.println(input);
} else if (Pattern.matches("^8[0-9]{10}$", input)) {
System.out.println("7" + input.substring(1));
} else if (Pattern.matches("^[0-9]{10}$", input)) {
System.out.println("7" + input);
} else {
System.out.println("Неверный формат номера");
}
}
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question