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";
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 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}$"))
{
System.out.println (line);
}
}
sc.close();
}
The phonebook.txt file contains: Name and number