Good day! Please help with the task. You need to write a function that takes a phone number and shows whether it is in the text. Phone number can be entered in different ways. The function was written and the regular expression seems to be correct. But the result is always the same "the number is NOT exist"

import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class PhoneNumber { public static void main(String[] args) throws IOException { Scanner in = new Scanner(System.in); System.out.println("Enter phone"); String phone = in.nextLine(); isPhone(phone); } public static boolean isPhone(String phone) throws IOException { Pattern p = Pattern.compile("^(\\s*)?(\\+)?([- _():=+]?\\d[- _():=+]?){10,14}(\\s*)?$"); Matcher editPhone = p.matcher(phone); String contents = getTextFromFile("/home/qa-1/HW"); String[] severalWords = breakIntoWords(contents); for (int i = 0; i < severalWords.length; i++) { if (editPhone.equals(severalWords[i])) { System.out.println("the number is exist"); return true; } } System.out.println("the number is NOT exist"); return false; } public static String getTextFromFile(String fileName) throws IOException { return new String(Files.readAllBytes(Paths.get(fileName))); } public static String[] breakIntoWords(String text) { text = text.replaceAll("[,;:.!?\\s-]+", " "); return text.split("\\s+"); } } 
  • not quite accurate posing the question ... what does it mean "differently"? phone number, how not to twist - it's numbers. Who prevents to perform a search by numbers only? - Dmitry
  • @ Dmitriy Aleksandrovich "On January 8, 2017, 22 students bought 34 chebureks and 12 bottles of beer" - rjhdby
  • I use this regexp "[7|8][ (-]?[\\d]{3}[ )-]?[\\d]{3}[ -]?[\\d]{2}[ -]?[\\d]{2}[\\D]" for Russian mobile numbers - rjhdby

2 answers 2

First you need to decide what characters the phone can contain. For different countries and cities the number of digits may vary.

Suppose we are interested in mobile phones of the Russian Federation.

The rules are:

  • the phone starts with 8 or +7
  • digits can be separated by a space and a symbol -
  • operator code can be enclosed in brackets

Accordingly, the first step is to remove these characters from the text, and the second is to select all sequences of 11 numbers, starting with 7 or 8:

 String text = "......."; String prepared = ","+text.replaceAll("[ -()]", "")+","; Matcher m = Pattern.compile("[^\\d]([78]\\d{10})[^\\d]") .matcher(prepared); while (m.find()) { System.out.println(m.group(1)); } 

This code, of course, can give false positives, or not to work when it is necessary on difficult cases, but the idea should be clear

    take this library https://mvnrepository.com/artifact/org.apache.commons/commons-lang3/3.7 it will expand the functionality of working with strings

    and do so, and then think what else can be optimized

     import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; import java.util.Scanner; import org.apache.commons.lang3.StringUtils; public class Test { public static void main(String[] args) throws IOException { String contents = getDigitFromFile("/home/qa-1/HW"); Scanner in = new Scanner(System.in); System.out.println("Enter phone"); System.out.println(isPhone(in.nextLine(), contents)); } public static String isPhone(String phone, String contents) { if (contents.contains(StringUtils.getDigits(phone))) return "the number is exist"; else return "the number is NOT exis"; } public static String getDigitFromFile(String fileName) throws IOException { return StringUtils.getDigits(new String(Files.readAllBytes(Paths.get(fileName)))); } 

    }