Regex for for Phone Numbers allowing for only 6 to 20 characters

Regex beginner here. I've been trying to tackle this rule for phone numbers to no avail and would appreciate some advice:

  1. Minimum 6 characters
  2. Maximum 20 characters
  3. Must contain numbers
  4. Can contain these symbols ()+-.
  5. Do not match if all the numbers included are the same (ie. 111111)

I managed to build two of the following pieces but I'm unable to put them together.

Here's what I've got:


Many thanks in advance!

3 answers

  • answered 2018-03-13 21:51 Nathan

    I'd go about it by first getting a list of all possible phone numbers (thanks @CAustin for the suggested improvements):

    lst_phone_numbers = re.findall('[0-9+()-]{6,20}',your_text)

    And then filtering out the ones that do not comply with statement 5 using whatever programming language you're most comfortable.

  • answered 2018-03-13 21:55 David

    Try this RegEx:



    1. (?: creates a non-capturing group
    2. (\d|[()+-]) creates a group to match a digit, parenthesis, +, or -
    3. (?!\1+$) this will not return a match if it matches the value found from #2 one or more times until the end of the string
    4. {6,20} requires 6-20 matches from the non-capturing group in #1

  • answered 2018-03-14 00:04 Mohammed Elhag

    Try this :


    So , this part ?!\2{5} means how many times is allowed for each one from the pattern to be repeated like this 22222 and i put 5 as example and you could change it as you want .