DreamThrall
Newbie
- Joined
- Oct 14, 2003
- Messages
- 3,483
- Reaction score
- 0
So I need to validate UK phone numbers, and based on what I found on Wikipedia's article on the UK phone number plan, this is what I've come up with:
This is C#'s version of regex, so it may be a little different, so I'll translate:
So basically:
(011x) xxx xxxx OR
(02x) xxxx xxxx
with the parenthesis being optional, and the spaces being optional or replaced by dashes
Any comments?
EDIT: Oh yeah, the answer is "yes", my powers of RegEx are godly.
Code:
^\(?(?<AreaCode>(?:(?<ac011>011\d)|(?<ac02>02\d)))[ \-)]?(?<TownCode>(?(ac011)\d{3})(?(ac02)\d{4}))[ \-]?(?<Number>\d{4})$
This is C#'s version of regex, so it may be a little different, so I'll translate:
- ^ Beginning of string
- \(? Optional opening parenthesis
- (?<AreaCode> begin named capture "AreaCode"
- (?: begin unnamed group
- (?<ac011> begin named capture "ac011" (for area codes beginning with "011")
- 011\d "011" followed by any digit
- | OR
- (?<ac02> begin named capture "ac02" (for area codes beginning with "02")
- 02\d "02" followed by any digit
- ) end capture/group
- [ \-)] optional space, dash or closing parenthesis
- (?<TownCode> begin capture for town code
- (?(ac011) if the named group "ac011" captured anything...
- \d{3} any digit x3
- ) end named capture
- (?(ac02) if the named capture "02" captured anything
- \d{4} any digit x4
- ) end capture
- $ end of string
So basically:
(011x) xxx xxxx OR
(02x) xxxx xxxx
with the parenthesis being optional, and the spaces being optional or replaced by dashes
Any comments?
EDIT: Oh yeah, the answer is "yes", my powers of RegEx are godly.