你好,我是正则表达式的新手,我不知道我在做什么.这就是我寻求帮助的原因.
我有以下电话号码359878123456,我需要检查电话号码是否符合标准.
> 359是确切的扩展名
>第二个2号码是操作符号码,以下是3个号码之一:87,88,89
>字符串中的下一个数字是2到9之间的数字
>和最后6个数字是0-9之间的任何数字
另外,我可以在检查中允许以下数字.
> 0878123456 – 其中0更改359的值
> 00359878123456 – 其中00改变了值
这就是我所处的位置
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>
我正在检查数字是否符合该标准,只打印该行.
这是我的代码.
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(); }
解决方法
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
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){} } }
结果
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