Skip to content
Advertisement

Regex to allow space between numbers or nothing before the first one

I have the method that follows – verifyPhones, I am using two regexs on it. The first one is to identify if the String is valid, if not I need to search which numbers are not valid.

My problem is when I have two valid numbers together – 20255501252025550125, the system is returning only one of them as wrong instead of the whole string.

How can I improve my regex to have achieve that?
Thanks in advance.

Definition of valid number:

Any number that have 9 numbers, separated or not by the char -
Example:

000-000-0000

0001110000

Here is my code:

 public static String verifyPhones(String phones) {
    Pattern patternValidAllPhones = Pattern.compile("^(((\d{3}[-]?){2}\d{4})[ ]+)+$");
    Pattern patternToFindWrongPhones = Pattern.compile("([ ]+((\d{3}[-]?){2}\d{4})[ ]+)");

    phones = phones.replaceAll("\r", " ").replaceAll("\n", " ").concat(" ");
    Matcher matcherValidAllPhones = patternValidAllPhones.matcher(phones);

    if(!matcherValidAllPhones.matches()) {
      Matcher matcherToFindWrongPhones = patternToFindWrongPhones.matcher(phones);
      return matcherToFindWrongPhones.replaceAll("").trim();
    }

    return "";
  }
@Test
  public void verifyPhonesTest_whenInvalidPhones_thenReturneInvalidPhones() {

    String invalidPhones1 = "202-555*0125 202-555-0125 202-555-0125 202-555-0125";
    String invalidPhones2 = "202-555-0125202-555-0125 202-555-0125 202-555-0125";
    String invalidPhones3 = "202555*0125 202-555-0125 202-555-0125 202-555-0125";
    String invalidPhones4 = "2025550125 20255501252025550125";

    String result1 = PhonesService.verifyPhones(invalidPhones1);
    String result2 = PhonesService.verifyPhones(invalidPhones2);
    String result3 = PhonesService.verifyPhones(invalidPhones3);
    String result4 = PhonesService.verifyPhones(invalidPhones4);

    assertFalse(result1.isEmpty());
    assertEquals("202-555*0125", result1);

    assertFalse(result2.isEmpty());
    assertEquals("202-555-0125202-555-0125", result2);

    assertFalse(result3.isEmpty());
    assertEquals("202555*0125", result3);

    assertFalse(result4.isEmpty());
    assertEquals("20255501252025550125", result4);
  }

Advertisement

Answer

Here’s what I suggest:

If valid numbers have to be separated by a space from each other, then you can first split the String by spaces into pieces, where each piece is going to be a number. And then apply validation pattern on each piece separately. Those pieces that do not match the pattern are going to be invalid numbers.

Here’s an example:

private static final Pattern phonePattern = Pattern.compile("(\d{3}[-]?){2}\d{4}");

public static List<String> verifyPhones(String phones) {
    String[] numbers = phones.split("\s+");
    List<String> wrongPhones = new ArrayList<>();
        
    for (String number : numbers) {
        if (!phonePattern.matcher(number).matches()) {
            wrongPhones.add(number);
        }
    }
    return wrongPhones;        
}

Note: I’ve changed the method’s signature. Now it returns a List of wrong numbers. You do not expect to always have only one invalid number, do you?

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement