How to check exact phone number in java with regex

Hello, I am new to regex and I don’t know what I am doing. That is why I am looking for help.

I have the following phone number +359878123456, and I need to check if the phone number matches the criteria.

  • +359 - exact expansion
  • the second 2 numbers are the numbers of the operators and one of the following 3 numbers: 87.88.89
  • the next number on the line is a number from 2 to 9
  • and the last 6 numbers are any number from 0 to 9

In addition, I can resolve the following numbers in check.

  • 0878123456 - where 0 changes the value +359
  • 00359878123456 - where 00 changes the value +

Here i am in

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ValidatePhoneNumber {
  public static void main(String[] argv) {

      String sPhoneNumber = "+35987";
      //String sPhoneNumber = "605-88899991";
      //String sPhoneNumber = "605-888999A";

      Pattern pattern = Pattern.compile("^\\+([3][5][9]){[87],[88],[89]}");
      Matcher matcher = pattern.matcher(sPhoneNumber);

      if (matcher.matches()) {
          System.out.println("Phone Number Valid");
      }
      else
      {
          System.out.println("Phone Number Not Valid");
      }
 }
}

EDITED>

I check that the numbers meet these criteria and only print this line. This is my code.

public static void main(String[] args) throws IOException {

        // Pattern pattern = Pattern.compile("((\\+|00)359|0)8[7-9][2-9]\\d{6}$"); //checks evaluation of phone number
         Pattern p = Pattern.compile("^((\\+|00)359|0)8[7-9][2-9]\\d{6}$");

         File inFile = new File ("phonebook.txt");

          Scanner sc = new Scanner (inFile);
          while (sc.hasNextLine())
          {
            String line = sc.nextLine();
            if(line.matches("^((\\+|00)359|0)8[7-9][2-9]\\d{6}$")) // here that code doesn't work
            {
            System.out.println (line);
            }
          }
          sc.close();
        }

The phonebook.txt file contains: Name and number

+4
3
import java.util.regex.*;
public class Test{
  public static void main(String[] args){
    String mob="+359878123456"; 
    Pattern p2 = Pattern.compile("^((\\+|00)359|0)(\\-|\\s)?8[7-9][2-9](\\-|\\s)?\\d{3}(\\s|\\-)?\\d{3}$");
    boolean b = p2.matcher("+359-878-123456").matches();
    if(b) System.out.println("True1");

    b = p2.matcher("+359 878 123 456").matches();
    if(b) System.out.println("True2");

    b = p2.matcher("+359-878-123-456").matches();
    if(b) System.out.println("True3");

    b = p2.matcher("+359-878123456").matches();
    if(b) System.out.println("True4");

    b = p2.matcher("00359-878123456").matches();
    if(b) System.out.println("True5");

    b = p2.matcher("0-878-123456").matches();
    if(b) System.out.println("True6");

    b = p2.matcher("+359878123456").matches();
    if(b) System.out.println("True7");

    b = p2.matcher("359878123456").matches(); // without + or 00
    if(b) System.out.println("True8");
    else System.out.println("FALSE8");

    b = p2.matcher("123478123456").matches(); 
    if(b) System.out.println("True9");
    else System.out.println("FALSE9");

  }
}

phonebook.txt

Sagar +359883123456
Test 00359883123565
Someone 0883123456
People 1234567890  // this will not be printed
Test1 +359873123456
Hide result

Sample.java

import java.util.*;
import java.util.regex.*;
import java.io.*;
public class Sample{
public static void main(String[] args){
 	try{
          File inFile = new File ("phonebook.txt");

          Scanner sc = new Scanner (inFile);
          while (sc.hasNextLine())
          {
            String line = sc.nextLine();
            if(line.matches("^((.)*\\s)?((\\+|00)359|0)8[7-9][2-9]\\d{6}$")) // here that code doesn't work
            {
            System.out.println (line);
            }
          }
          sc.close();
}catch(Exception e){}
        }
}
Hide result

C:\Users\acer\Desktop\Java\programs>javac Sample.java

C:\Users\acer\Desktop\Java\programs>java Sample
Sagar +359883123456
Test 00359883123565
Someone 0883123456
Test1 +359873123456
Hide result
+5

:

Pattern pattern = Pattern.compile("^(((\\+|00)359)|0)8[7-9][2-9][0-9]{6}$");

ideone.

+4

I think you need to add 6 numbers between 0-9, something like this

    public static void main(String ...args){
      String phoneNumber="+359878123456";

      Pattern pattern = Pattern.compile("^(\\+|0|00)3598[789][2-9][0-9]{6}");
      Matcher matcher = pattern.matcher(phoneNumber);
      System.out.println(matcher.matches());
}
+1
source

Source: https://habr.com/ru/post/1669815/


All Articles