-1

i want to make the regex for allowing phone number from 7 to 26 digits but except 8 and 9 digit.

I tried is this one but its not working :

^\d(?:[-\s]?\d){6,}((?!\d(?:[-\s]?\d){7}).)((?!\d(?:[-\s]?\d){8}).)((?!\d(?:[-\s]?\d){9,26}).)$

Allowed Input : Numeric 7 to 26 (but not 8 or 9 digits)

  • 1234567
  • 1234567890

Not allowed input :

  • 12
  • 123
  • 1234
  • 123456
  • 12345678
  • L123456789
Unihedron
  • 10,902
  • 13
  • 62
  • 72
spuser
  • 1
  • 3

3 Answers3

0

I guess this regex will do the job:

^(\d{7}|\d{10,26})$

Try the demo

Unihedron
  • 10,902
  • 13
  • 62
  • 72
Kristijan Iliev
  • 4,901
  • 10
  • 28
  • 47
0

You can use:

\b(\d{7}(?:\d{3,19})?)\b

RegEx Demo

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • 1
    Just out of curiosity, why are you using the empty alternative trick instead of making the group optional? And why did you use `23` as your upper bound? Shouldn't it be `19`? And why are you using word boundaries when the OP used anchors? – Alan Moore Nov 03 '14 at 20:31
  • 1
    Thanks @AlanMoore: It should have been 19 instead of 23 (don't know what I was thinking at that time). I also made last part optional instead of using an empty match. – anubhava Nov 03 '14 at 20:33
0

Your regex allows for or a space or hyphen between any pair of digits, is that a requirement? Assuming it is, here's what I would use:

^\d(?!(?:[-\s]?\d){7,8}$)(?:[-\s]?\d){6,25}$

I think the biggest problem with your regex is the placement of the lookaheads. They have to be used at the beginning of the regex to do any good. What's happening in your regex is that the \d(?:[-\s]?\d){6,} consumes all the digits it can, then the lookaheads are applied at the end of the string, where there are no more characters of any kind. So, being negative lookaheads, they always succeed.

Another problem is the dot (.) following each lookahead; because they're not inside the lookaheads, and the enclosing groups are not optional, each dot has to consume one character, which is not accounted for by any of the range quantifiers (especially the last one, {9,26}), and is not limited to matching just a digit, hyphen or space.

It looks like you're trying to use the captive lookahead idiom (as I call it) demonstrated in this answer. It's not useful in this case.

Community
  • 1
  • 1
Alan Moore
  • 73,866
  • 12
  • 100
  • 156