-1

I have a pattern that I am using and it does everything I need it to except for it does not allow for an 10 digit phone number and not all UK numbers are 11 digits, for example, 01932 783433 is allowed and valid but 01932 78383 is also valid but not allowed;

Pattern:

^\s*\(?(020[7,8]{1}\)?[ ]?[1-9]{1}[0-9{2}[ ]?[0-9]{4})|(0[1-8]{1}[0-9]{3}\)?[ ]?[1-9]{1}[0-9]{2}[ ]?[0-9]{3})\s*$

I've tried without success to edit the pattern but each time I make a change and think I'm there the pattern starts allowing non numeric characters like 'ext 456' (extension numbers) at the end which is what I am trying to stop.

Is anyone able to help with a solution to make the 11th digit optional without changing anything else?

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
sbendall
  • 3
  • 2
  • 2
    Change the last `{3}` to `{2,3}` – Niet the Dark Absol Nov 09 '20 at 13:55
  • You should not overuse character classes. Due to the unnecessary `[]` around space, you overlooked `[0-9{2}[ ]?` typo. Your two alternatives are not grouped correctly, you still match strings like `awjfhwe01932 783433`. Is `(0207)119 327` like number valid? Is yes, use `^\s*(?:\(?020[78]\)? ?[1-9][0-9]{2} ?[0-9]{3,4}|0[1-8][0-9]{3}\)? ?[1-9][0-9]{2} ?[0-9]{2,3})\s*$`, see https://regex101.com/r/xCnLCC/1 – Wiktor Stribiżew Nov 09 '20 at 14:00

1 Answers1

-1

The comments of changing {3} to {2,3} resolve the issue I was having and the pointer to regex101.com were very useful in understanding my issue.

sbendall
  • 3
  • 2