Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check exact phone number in Java with regex

Hello I am new with regex and i don't have a clue what I am doing. That is why i am seeking for assistance.

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

  • +359 is the exact extension
  • second 2 numbers are the operator numbers and one of the following 3 numbers: 87,88,89
  • next number in the string is a number from 2 to 9
  • and the last 6 numbers are any number from 0-9

Additionally i can allow the following numbers in the checking.

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

This is where I am at

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 am checking the numbers are matching that criteria and only print that 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 file phonebook.txt contains: Name and number

like image 480
6 revs, 3 users 95% Avatar asked Dec 23 '22 20:12

6 revs, 3 users 95%


1 Answers

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");

  }
}

Updated

phonebook.txt

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

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){}
        }
}

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
like image 189
Sagar V Avatar answered Dec 26 '22 10:12

Sagar V