英文:
The method doesn't work correctly when using matches (), regex, trying to validate Phone number
问题
下午好,我需要编写一个方法来验证电话号码。
我需要这个号码只能包含数字(0-9)和这些字符: "()" "-" "+"
,其他任何字符都是不允许的!
例如:
+210-998-234-01234 -可以
210-998-234-01234-可以
+21099823401234-可以
+210-998-234-01234 -可以
+210-(998)-(234)-(01234) - 可以
+210-99A-234-01234 - 错误,+210-999-234-0""234 -错误,+210-999-234-02;4 - 错误
如果号码包含未解析的内容,它应该抛出异常。
我编写了这个方法,但出于某种原因它不能正常工作。
请解释一下我在使用 matches() 时犯了什么错误。
非常感谢你的帮助!
public void validatePhoneNumber(String phoneNumber) {
if (!phoneNumber.matches("[ˆ0-9()\\-+]")) {
throw new IncorrectlyEnteredDataException("电话号码包含未解析的字符");
}
}
英文:
Good afternoon, I need to write a method that will validate the phone number .
I need the number can only contain numbers (0-9) and these characters : "()" "-" "+"
, but everything else is forbidden!
For example :
+210-998-234-01234 -OK
210-998-234-01234-OK
+21099823401234-OK
+210-998-234-01234 -OK
+210-(998)-(234)-(01234) - OK
+210-99A-234-01234 - FALSE , +210-999-234-0""234 -FALSE , +210-999-234-02;4 - FALSE
If the number contains something unresolved it should throw exception.
I wrote this method but for some reason it doesn't work right.
Please explain where I made a mistake when using matches()
I will be grateful for your help!
public void validatePhoneNumber(String phoneNumber) {
if (!phoneNumber.matches("[ˆ0-9()\\-+]")) {
throw new IncorrectlyEnteredDataException("Phone number have unresolved characters");
}
答案1
得分: 0
这个正则表达式适用于您的电话号码:
"^\\+?\\d{3}(?:\\d{11}|(?:-\\d{3,}){3}|(?:-\\(\\d{3,}\\)){3})"
但是如果您想要匹配各种变化的电话号码,可以参考这个解决方案 - 在这个解决方案中,Java为有效的电话号码生成正则表达式:
在测试台和上下文中显示的正则表达式:
public class TestBench {
private final static Pattern VALID_PHONE_NUMBER_PATTERN =
Pattern.compile("^\\+?\\d{3}(?:\\d{11}|(?:-\\d{3,}){3}|(?:-\\(\\d{3,}\\)){3})");
public static void main (String[] args) {
List<String> inputNumbers = Arrays.asList(
"+210-998-234-01234", // 通过
"210-998-234-01234", // 通过
"+21099823401234", // 通过
"+210-(998)-(234)-(01234)", // 通过
"+210-99A-234-01234", // 失败
"+210-999-234-0\"234", // 失败
"+210-999-234-02;4", // 失败
"-210+998-234-01234", // 失败
"+210-998)-(234-(01234" // 失败
);
for (String number : inputNumbers) {
validatePhoneNumber(number);
}
}
private static void validatePhoneNumber(String phoneNumber) {
Matcher matcher = VALID_PHONE_NUMBER_PATTERN.matcher(phoneNumber);
if(matcher.matches()) {
System.out.println("This is a valid phone number: " + matcher.group());
}
}
}
输出:
This is a valid phone number: +210-998-234-01234
This is a valid phone number: 210-998-234-01234
This is a valid phone number: +21099823401234
This is a valid phone number: +210-(998)-(234)-(01234)
注释:
注意:您可以通过在正则表达式中更改X和Y来改变可以容忍的数字数量,有关详细信息,请参见下面的说明:
"^\\+?\\d{3}(?:\\d{X}|(?:-\\d{Y,}){3}|(?:-\\(\\d{Y,}\\)){3})"
在建议的正则表达式中,我们假设在出现其他变化之前,始终会有三个数字。
英文:
This regex will work for your phone numbers:
"^\\+?\\d{3}(?:\\d{11}|(?:-\\d{3,}){3}|(?:-\\(\\d{3,}\\)){3})"
But if you want to match all kind of variations of phone numbers, then see this solution - where Java generates REGEX for valid phone numbers:
Regex shown in test bench and context:
public class TestBench {
private final static Pattern VALID_PHONE_NUMBER_PATTERN =
Pattern.compile("^\\+?\\d{3}(?:\\d{11}|(?:-\\d{3,}){3}|(?:-\\(\\d{3,}\\)){3})");
public static void main (String[] args) {
List<String> inputNumbers = Arrays.asList(
"+210-998-234-01234", // PASS
"210-998-234-01234", // PASS
"+21099823401234", // PASS
"+210-(998)-(234)-(01234)", // PASS
"+210-99A-234-01234", // FAIL
"+210-999-234-0\"\"234", // FAIL
"+210-999-234-02;4", // FAIL
"-210+998-234-01234", // FAIL
"+210-998)-(234-(01234" // FAIL
);
for (String number : inputNumbers) {
validatePhoneNumber(number);
}
}
private static void validatePhoneNumber(String phoneNumber) {
Matcher matcher = VALID_PHONE_NUMBER_PATTERN.matcher(phoneNumber);
if(matcher.matches()) {
System.out.println("This is a valid phone number: " + matcher.group());
}
}
}
Output:
This is a valid phone number: +210-998-234-01234
This is a valid phone number: 210-998-234-01234
This is a valid phone number: +21099823401234
This is a valid phone number: +210-(998)-(234)-(01234)
Comment:
Notice: You can change on how many numbers you can tolerate by changing X and Y in the regex and so on, see below for clarification:
"^\\+?\\d{3}(?:\\d{X}|(?:-\\d{Y,}){3}|(?:-\\(\\d{Y,}\\)){3})"
In the regex suggested, we asume that there will always be three digits first before another variation comes.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论