我想像这样匹配电话号码
它应该有3 digits except 000,666
以及之间的任何数字900-999
后面加“-”,然后2 digits后面加“-”,然后4 digits.
例如:
123-75-3456 is a match
000- 23-3452 is not a match (no 000)
915-23-4534 is not a match (greater than 900)
这就是我目前的立场:
[0-9&&^[000,666,[900-999]]{3}-[0-9]{2}-[0-9]{4}
我认为这个应该可以解决问题:
^(?!000|666|9\d{2})\d{3}-\d{2}-\d{4}$
编辑:我发现否定的前瞻语法这个线程 https://stackoverflow.com/questions/2078915/a-regular-expression-to-exclude-a-word-string.
编辑2:这里有一个小代码片段,供那些想要测试它的人使用:
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("^(?!000|666|9\\d{2})\\d{3}-\\d{2}-\\d{4}$");
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("Next number :");
Matcher matcher = pattern.matcher(sc.nextLine());
if (matcher.find()) {
System.out.println("Matches");
} else {
System.out.println("Doesn't match");
}
}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)