Reputation: 179
I am studying regexes and wanted to know how to check if a string contains a precise amount of number characters inside despite the fact that they may be divided but other kinds of characters.
Here I have the method that checks if string follows those parameters:
1)if string starts with '+', then this string has to have precisely 12 number
2)if string starts with '(' or number, then string has to have precisely 10 numbers
3)string may contain 0-2 '-' characters that can't go one after another Example: '--'
4)string may contain 1 pair of brackets '(' and ')', but it is located on the left from '-'
5)brackets has to have 3 numbers in between
6)string does not contain non-number characters inside
7)string ends with number
Here is the method itself:
public static boolean checkTelNumber(String number) {
if (number.matches("\\d{12} | \\d{10}"))
return number.matches("^\\+?(\\d*(\\(\\d{3}\\))?\\d*(\\-?\\d+\\-?)?\\d*)");
else return false;
}
This method correctly checks for everything except if a string has 10/12 numbers inside if you remove if (number.matches("\\d{12} | \\d{10}"))
part of it. Thanks in advance!
All my output:
method output | number of the result | actual result | number I used to check
true - 1 true +380501234567
true - 2 true +38050123-45-67
true - 3 true +38050123-4567
true - 4 true +3805012345-67
true - 5 true +38(050)1234567
true - 6 true +38(050)123-45-67
true - 7 true +38(050)12345-67
true - 8 true +38(050)123-4567
true - 9 true +-313450531202
true - 10 true +38051202(345)7
true - 11 true +38051202(345)-7
false - 12 false +38050)1234567
false - 13 false +38050)123-45-67
false - 14 false +38050)123-4567
false - 15 false +38050)12345-67
false - 16 false +38(0501234567
false - 17 false +38(050123-45-67
false - 18 false +38(05012345-67
false - 19 false +38(050123-4567
true - 20 false +38(050)12-3456
true - 21 false +38(050)123456
true - 22 false +38(050)12345-6
true - 23 false +38(050)1-23456
false - 24 false +38)050(1234567
false - 25 false +38(050)1-23-45-6-7
false - 26 false +38-(050)1234567
false - 27 false +38((050)1234567
false - 28 false +5(0--5)1234567
false - 29 false +-313450531202))
false - 30 false +38051202(345)-7))
true - 1 true 050123-4567
true - 2 true 050123-45-67
true - 3 true 05012345-67
true - 4 true (999)1112222
true - 5 true (999)111-22-22
true - 6 true (999)111-2222
true - 7 true (999)11122-22
true - 8 true 1-23456789-0
true - 9 true (345)0512027
false - 10 false (345)0512027))
false - 11 false 1-23456789-0))
false - 12 false 7-4-4123689-5
false - 13 false 050ххх4567
true - 14 false 050123456
false - 15 false (0)501234567
Upvotes: 1
Views: 1404
Reputation:
Somewhat tested not for the feint hearted!!
Symbiotic validation insures the proper form in the match.
^(?=\+\D*(?:\d\D*){12}$|(?=[(\d])\D*(?:\d\D*){10}$)(?!.*--)(?!.*-.*\(.*\))(?=[+\d()]*-?[\d()]*-?[\d()]*$)\+?[-\d]*(?:\(\d{3}\))?[-\d]*\d$
Explained
^ # BOS
# ASSERTs - Validation
(?=
\+ # if +, 12 digits
\D*
(?: \d\D* ){12}
$
| # or,
(?= [(\d] ) # if ( or \d, 10 digits
\D*
(?: \d\D* ){10}
$
)
(?! .* -- ) # optional -, but not 2 in a row
(?! .* - .* \( .* \) ) # optional (ddd) but not after -
(?= [+\d()]* -? [\d()]* -? [\d()]* $ ) # max 2 dashes
# MATCH
\+? # optional +
[-\d]*
(?: \( \d{3} \) )? # optional (ddd)
[-\d]* # dash or numbers
\d # ends with number
$ # EOS
Upvotes: 1