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.
Additionally i can allow the following numbers in the checking.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With