The method doesn't work correctly when using matches (), regex, trying to validate Phone number

huangapple go评论72阅读模式
英文:

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为有效的电话号码生成正则表达式:

https://stackoverflow.com/questions/123559/how-to-validate-phone-numbers-using-regex/63771966#63771966

在测试台和上下文中显示的正则表达式:

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:

&quot;^\\+?\\d{3}(?:\\d{11}|(?:-\\d{3,}){3}|(?:-\\(\\d{3,}\\)){3})&quot;

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:

https://stackoverflow.com/questions/123559/how-to-validate-phone-numbers-using-regex/63771966#63771966

Regex shown in test bench and context:

public class TestBench {

        private final static Pattern VALID_PHONE_NUMBER_PATTERN =
                Pattern.compile(&quot;^\\+?\\d{3}(?:\\d{11}|(?:-\\d{3,}){3}|(?:-\\(\\d{3,}\\)){3})&quot;);

        public static void main (String[] args) {
            List&lt;String&gt; inputNumbers = Arrays.asList(
                    &quot;+210-998-234-01234&quot;,       // PASS
                    &quot;210-998-234-01234&quot;,        // PASS
                    &quot;+21099823401234&quot;,          // PASS
                    &quot;+210-(998)-(234)-(01234)&quot;, // PASS
                    &quot;+210-99A-234-01234&quot;,       // FAIL
                    &quot;+210-999-234-0\&quot;\&quot;234&quot;,    // FAIL
                    &quot;+210-999-234-02;4&quot;,        // FAIL
                    &quot;-210+998-234-01234&quot;,       // FAIL
                    &quot;+210-998)-(234-(01234&quot;     // 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(&quot;This is a valid phone number: &quot; + 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:

&quot;^\\+?\\d{3}(?:\\d{X}|(?:-\\d{Y,}){3}|(?:-\\(\\d{Y,}\\)){3})&quot;

In the regex suggested, we asume that there will always be three digits first before another variation comes.

huangapple
  • 本文由 发表于 2020年9月7日 01:45:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/63767154.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定