开发者

matching phone number in regular expression

[1-9]\\d{2}-[1-9]\\d{2}-\\d{4}

What does the [1-9] do? is that like a specific range of integer? I tried 194-333-1111 but doesn't validate.

This is a trivial qu开发者_如何学编程estions but took me an hour and still can't figure out.

Any help is appreciated!!! Thanks


Edit

if (phone.matches("[1-9]\\d{2}-[1-9]\\d{2}-\\d{4}"))
  System.out.println("Invalid phone number");
else
  System.out.println("Valid input. Thank you.");


[1-9] matches the range of characters between 1 and 9, inclusive.

Where did you test the expression, because it does match your target string. However the slashes are escaped, as they need to be when entered in may programing languages. It is possible that you tested the expression with an application that will do the escaping for you.

EDIT for code:

You have your error messages reversed. matches() returns true when the string is valid, but you are printing that it is invalid in the true part of the if else statement.


[1-9] matches any digit starting from 1 to 9

The given regex is surely wrong unless you mean \\d as \d and will not match the number given by you. If you can tell what numbers format you want to match, we can give better regex.


If you can depend on other libraries then I'd suggest you use the libphonenumber open source library from Google to validate your phone number. It has validation support as well.


Your regular expression, as it's written, probably won't do what you want it to. You'll need to unescape the backslashes first. For example, in Perl you'd use it like:

if ($number =~ /[1-9]\d{2}-[1-9]\d{2}-\d{4}/) {
  print "matches!\n";
}

Your regex would then break down as follows:

/[1-9]    # Match exactly one of the numbers 1 through 9
 \d{2}    # Match exactly two digits
 -        # Match exactly one dash
 [1-9]    # Match exactly one of the numbers 1 through 9
 \d{2}    # Match exactly two digits
 -        # Match exactly one dash
 \d{4}    # Match exactly four digits
/x

Edit: To show you how your regex as it currently stands works, here's its breakdown:

/[1-9]  # Match exactly one of the numbers 1 through 9
 \\     # Match exactly one \
 d{2}   # Match exactly two 'd's
 -      # Match exactly one dash
 [1-9]  # Match exactly one of the numbers 1 through 9
 \\     # Match exactly one \
 d{2}   # Match exactly two 'd's
 -      # Match exactly one dash
 \\     # Match exactly one \
 d{4}   # Match exactly four 'd's
/x

See how much of a difference the double backslashes make?


Given below is breakup of the Regex:

[1-9] // Starts with a digit other than 0
\d{2} // and followed by any two digits
- // and followed by -
[1-9] // and followed a digit other than 0
\d{2} // and followed by any two digits
- // and followed by -
\d{4} // and followed by any four digits 

194-333-1111 matches the above Regex. The issue could be with the escaping character.

e.g:

public static void RegexTest()
    {
            Pattern p = Pattern.compile("[1-9]\\d{2}-[1-9]\\d{2}-\\d{4}");
            Matcher m = p.matcher("194-333-1111");
            boolean b = m.matches();
          System.out.println(b);

    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜