Well, I'm still following the creepy path of using regexes, as I'm interested in this topic and trying to master it this way:). I've tried to make my code account for all the suggested cases and tested it with all the suggested phone numbers plus some of mine. It finally produces the expected output, but the validator says there are some /*mysterious*/ verification criteria I haven't yet accounted for. I'm just wondering what other cases I need to include, or what I might have failed to notice in the task description.
package com.codegym.task.task22.task2212;
/*
Phone number verification
*/
public class Solution {
public static boolean checkPhoneNumber(String phoneNumber) {
boolean isValid = false;
// null check, "" check, check for letters (crit 6)
if (phoneNumber == null || phoneNumber.isEmpty() || phoneNumber.matches(".*[a-zA-Z].*")) return isValid;
if (!Character.isDigit(phoneNumber.charAt(phoneNumber.length()-1))) return isValid;
// check for ending with a digit (crit 7)
else {
//if (phoneNumber.matches(".*//d$")) {
// case: start with a plus (criterion 1)
// if (phoneNumber.matches("^\\+(.*\\d.*){12}$")) {
if (phoneNumber.startsWith("+") && phoneNumber.matches("(.*\\d.*){12}$")) {
//1) + and 12 digits
//2) plus, two brackets, twelve digits
//3) plus, two brackets, twelve digits, one dash
//4) plus, two brackets, twelve digits, two dashes
//5) plus, one dash, 12 digits
//6) plus, 2 non-consecutive dashes, 12 digits
if (phoneNumber.matches("^\\+\\d*(\\(\\d{3}\\))?(?!.*--)(?!.*-.*-.*-)[\\d-]*\\d*$")) {
if (phoneNumber.matches("^[^()]*\\([^()]*$") || phoneNumber.matches("^[^()]*\\)[^()]*$")
|| phoneNumber.matches("^[^()]*\\)[^()]*\\([^()]*$")) isValid = false;
else isValid = true;
}
}
// check for a start with an opening parentheses or a digit (crit 2)
else if (phoneNumber.startsWith("(") || Character.isDigit(phoneNumber.charAt(0))) {
if (phoneNumber.matches( "^(.*\\d.*){10}$")) {
if (phoneNumber.matches("^.*(\\(\\d{3}\\d\\))?(?!.*--)(?!.*-.*-.*-)[\\d-]*\\d*$")) { //mine
if (phoneNumber.matches("\\([^()]*$")
|| phoneNumber.matches("^[^()]*\\([^()]*")
|| phoneNumber.matches("^[^()]*\\)[^()]*$")
|| phoneNumber.matches("^[^)]*\\)[^()]*\\([^()]*$")
|| phoneNumber.matches("^.*\\)-.*")
) isValid = false;
else isValid = true;
}
}
}
}
return isValid;
}
public static void main (String[] args) {
System.out.println("0: " + checkPhoneNumber("(000)-896-0000") + " must be false"); // - false
System.out.println("1: " + checkPhoneNumber("+380501234567") + " must be true"); // - true
System.out.println("2: " + checkPhoneNumber("+38(050)1234567") + " must be true"); // - true
System.out.println("3: " + checkPhoneNumber("+38(050)123-4567") + " must be true"); // - true
System.out.println("4: " + checkPhoneNumber("+8(000)3-4") + " must be false"); // - false
System.out.println("5: " + checkPhoneNumber("+38(050)123-45-67") + " must be true"); // - true
System.out.println("6: " + checkPhoneNumber("+38(050)123--4567") + " must be false"); // - false
System.out.println("7: " + checkPhoneNumber("+38050123-45-67") + " must be true"); // - true
System.out.println("8: " + checkPhoneNumber("050123-4567") + " must be true"); // - true
System.out.println("9: " + checkPhoneNumber("+38)050(1234567") + " must be false"); // - false
System.out.println("10: " + checkPhoneNumber("+38050(1234567") + " must be false"); // - false
System.out.println("11: " + checkPhoneNumber("+38(050)1-23-45-6-7") + " must be false"); // - false
System.out.println("12: " + checkPhoneNumber("050xxx4567") + " must be false"); // - false
System.out.println("13: " + checkPhoneNumber("050123456") + " must be false"); // - false
System.out.println("14: " + checkPhoneNumber("(000)1234567") + " must be true"); // - true
System.out.println("15: " + checkPhoneNumber("+38-050-1-234-56-7890") + " must be false"); // - false
}
}
\\d+\\.?\\d*
Last but not least a test: done. As said, the mean thing here is the counting and depending on the count you need to modify the regex. That you can do like: count1: mainRegex, count2: prefixRegex + mainRegex