[英]Java Regular Expressions to Validate phone numbers
在下面的代碼中,我試圖讓輸出成為電話號碼的不同格式,以及它是否有效。 我想出了一切,但第 11 行的 Java 正則表達式代碼是字符串模式。
import java.util.regex.*;
public class MatchPhoneNumbers {
public static void main(String[] args) {
String[] testStrings = {
/* Following are valid phone number examples */
"(123)4567890", "1234567890", "123-456-7890", "(123)456-7890",
/* Following are invalid phone numbers */
"(1234567890)","123)4567890", "12345678901", "(1)234567890",
"(123)-4567890", "1", "12-3456-7890", "123-4567", "Hello world"};
// TODO: Modify the following line. Use your regular expression here
String pattern = "^/d(?:-/d{3}){3}/d$";
// current pattern recognizes any string of digits
// Apply regular expression to each test string
for(String inputString : testStrings) {
System.out.print(inputString + ": ");
if (inputString.matches(pattern)) {
System.out.println("Valid");
} else {
System.out.println("Invalid");
}
}
}
}
基本上,您需要采用 3 或 4 種不同的模式並將它們與“|”組合在一起:
String pattern = "\\d{10}|(?:\\d{3}-){2}\\d{4}|\\(\\d{3}\\)\\d{3}-?\\d{4}";
\\d{10}
匹配 1234567890(?:\\d{3}-){2}\\d{4}
匹配 123-456-7890\\(\\d{3}\\)\\d{3}-?\\d{4}
匹配 (123)456-7890 或 (123)4567890String str= "^\\s?((\\+[1-9]{1,4}[ \\-]*)|(\\([0-9]{2,3}\\)[ \\-]*)|([0-9]{2,4})[ \\-]*)*?[0-9]{3,4}?[ \\-]*[0-9]{3,4}?\\s?";
if (Pattern.compile(str).matcher(" +33 - 123 456 789 ").matches()) {
System.out.println("yes");
} else {
System.out.println("no");
}
您需要的正則表達式是:
String regEx = "^\\(?(\\d{3})\\)?[- ]?(\\d{3})[- ]?(\\d{4})$";
正則表達式解釋:
^\\\\(?
- 可以以選項 "("
(\\\\d{3})
- 后跟 3 位數字
\\\\)?
- 可能有一個可選的“)”
[- ]?
- 在前 3 位數字或可選的 ) 字符之后可能有一個可選的“-”
(\\\\d{3})
- 后跟 3 位數字。
[- ]?
- 數字后可能有另一個可選的“-”
(\\\\d{4})$
- 以四位數字結尾
考慮到有關電話號碼格式的這些事實:-
String allCountryRegex = "^(\\+\\d{1,3}( )?)?((\\(\\d{1,3}\\))|\\d{1,3})[- .]?\\d{3,4}[- .]?\\d{4}$";
讓我們打破正則表達式並理解,
^
表達開始(\\\\+\\\\d{1,3}( )?)?
是 1 到 3 位數字之間的國家代碼的可選匹配,前綴為“+”符號,后跟空格或無空格。((\\\\(\\\\d{1,3}\\\\))|\\\\d{1,3}
是 1 到 3 位數字的強制性組,帶或不帶括號,后跟連字符、空格或不帶空格。\\\\d{3,4}[- .]?
是由 3 或 4 位數字組成的強制性組,后跟連字符、空格或無空格\\\\d{4}
是最后 4 位的強制性組$
表達式結束此正則表達式模式匹配大多數國家/地區的電話號碼格式,包括:-
String Afghanistan = "+93 30 539-0605";
String Australia = "+61 2 1255-3456";
String China = "+86 (20) 1255-3456";
String Germany = "+49 351 125-3456";
String India = "+91 9876543210";
String Indonesia = "+62 21 6539-0605";
String Iran = "+98 (515) 539-0605";
String Italy = "+39 06 5398-0605";
String NewZealand = "+64 3 539-0605";
String Philippines = "+63 35 539-0605";
String Singapore = "+65 6396 0605";
String Thailand = "+66 2 123 4567";
String UK = "+44 141 222-3344";
String USA = "+1 (212) 555-3456";
String Vietnam = "+84 35 539-0605";
來源: https : //codingnconcepts.com/java/java-regex-for-phone-number/
為括號中的三位數字或三位數字(帶有可選的破折號)創建一個非捕獲組。 然后你需要三個數字(另一個可選的破折號),然后是四個數字。 比如, (?:\\\\(\\\\d{3}\\\\)|\\\\d{3}[-]*)\\\\d{3}[-]*\\\\d{4}
。 你可能會使用Pattern
。 大家一起喜歡,
String[] testStrings = {
/* Following are valid phone number examples */
"(123)4567890", "1234567890", "123-456-7890", "(123)456-7890",
/* Following are invalid phone numbers */
"(1234567890)","123)4567890", "12345678901", "(1)234567890",
"(123)-4567890", "1", "12-3456-7890", "123-4567", "Hello world"};
Pattern p = Pattern.compile("(?:\\(\\d{3}\\)|\\d{3}[-]*)\\d{3}[-]*\\d{4}");
for (String str : testStrings) {
if (p.matcher(str).matches()) {
System.out.printf("%s is valid%n", str);
} else {
System.out.printf("%s is not valid%n", str);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.