mobstar
mobstar

Reputation: 1

Regular Expression catching uk phone number

I trying to create a regular expression to catch the following conditions, but totally failing to get my head around it (Friday) and need a bit of help please?

Trying to capture UK phone numbers starting with area code or no area code, but excluding mobiles.

example: 01316691234 or 6691234 but not any number starting with 07

got this so far ^[0-9]1?(\d{6,11}) but struggling to exclude the 07 numbers.

Upvotes: 0

Views: 61

Answers (2)

Bohemian
Bohemian

Reputation: 424993

Use a negative look ahead to prevent numbers starting with "07" matching:

^(?!07)([0-9]1)?(\d{6,11})

Upvotes: 0

Mitya
Mitya

Reputation: 34556

This is based on the supposition that UK area codes:

  • start with 0 and are followed by 1 (usual) or 2 (London);
  • run to 3-5 digits
  • are followed by a phone number 6-7 digits long

Whilst this seems sound to me, I'm no telecoms anorak so you'll need to modify accordingly if any part of this supposition is wrong:

/^(0[12345689]\d{1,3} ?)?\d{6,7}$/

Either way, it's a bit of a can of worms. Postscodes and phone numbers don't lend themselves well to REGEX; the more tightly you refine it, the more at risk you are from new rules being added tomorrow - e.g. if they launched a new area code starting 03.

Upvotes: 1

Related Questions