1

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
11
  • Please provide sample correct and incorrect input to help clarify the requirement and make answers easier to verify. Commented Mar 9, 2017 at 0:15
  • This doesn't really have a question in it. Is the question "What's a regex that will match strings that follow parameters 1 - 7?", or is it "What's a regex that matches strings containing exactly 10 (or 12) digits anywhere in a string?" Commented Mar 9, 2017 at 0:15
  • 1
    All of these rules make the regex hard. A simple loop over the characters would be easier. Commented Mar 9, 2017 at 0:16
  • If I had to fix a program with regexes, like the above, all over, I would probably quit Commented Mar 9, 2017 at 0:21
  • 1
    I read this 5 times before realizing it's describing a spec for a telephone number. Commented Mar 9, 2017 at 0:22

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

2 Comments

As always, if it answers your question or helps, upvote.
it worked perfectly, need some time to process and figure out what is what, thanks a lot!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.