b3ta.com qotw
You are not logged in. Login or Signup
Home » Question of the Week » Off Topic » Post 441790 | Search
This is a question Off Topic

Are you a QOTWer? Do you want to start a thread that isn't a direct answer to the current QOTW? Then this place, gentle poster, is your friend.

(, Sun 1 Apr 2001, 1:00)
Pages: Latest, 836, 835, 834, 833, 832, ... 1

« Go Back | Popular

My brain is dribbling out of my ears...
regular expressions have finally done for me

What am I trying to do?

convert a telephone number from
01234-567890
to
+44(01234)567890 (canonical)

And it doesn't work

Any uber geeks know (or can work out) the magic patterns to make this happen - I'd love you forever
(, Sat 6 Jun 2009, 22:17, 7 replies, latest was 16 years ago)
dont dial the first 0

(, Sat 6 Jun 2009, 22:20, Reply)
00441234567890
Sorted
(, Sat 6 Jun 2009, 22:21, Reply)
+441234567890

(, Sat 6 Jun 2009, 22:37, Reply)
Wooho it works
maybe I should have specified that this is by computer code...

However, thanks for the answers...

match pattern = "(\d{0.5})
replace pattern = "+44($1)"

seems to do the business
(, Sat 6 Jun 2009, 22:38, Reply)
How complex do you want to make this?
I havent tried yours out but how will it handle the 4 different main number formats I can think of:

020 XYYY YYYY - London (Where X = 7, 8 or 3)
01X1 YYY YYYY - Other Major City
01XXX YYY YYY - Towns
07XYY YYY YYY - Mobile (Where X = 7, 8 or 9)

01234-567890 to +44(01234)567890 would work fine but if you do a London one do you get:
020-71234567 to +44(02071)234567 (Incorrect) or do you get +44(020)71234567 (Correct)

Equally a Birmingham number of 0121-1234567, do you get:
+44(01211)234567 (Incorrect) or +44(0121)1234567 (Correct)

Also a Mobile should never have anything other than the first 0 in brackets as the rest is never optional:
+44(07712)345678 (Incorrect)
+44(0)7712345678 (Correct)

Actually now that I think about it it would probably be both more correct and easier to code to just to a straight swap of the initial 0 to +44(0) as If you're needing to add the country code chances are you aren't in the same area code so will need to dial the rest regardless. In other words forget all the times above where I say correct and just change them all to:
+44(0)2071234567
+44(0)1211234567
+44(0)1234567890
+44(0)7712345678

The above it closer to the recommended format and with some modifications you might want to have a look at
regexlib.com/REDetails.aspx?regexp_id=593 for this one:
^(((\+44\s?\d{4}|\(?0\d{4}\)?)\s?\d{3}\s?\d{3})|((\+44\s?\d{3}|\(?0\d{3}\)?)\s?\d{3}\s?\d{4})|((\+44\s?\d{2}|\(?0\d{2}\)?)\s?\d{4}\s?\d{4}))(\s?\#(\d{4}|\d{3}))?$

UK phone number regular expression. Allows +44 national code in addition to extension numbers of 3 or 4 digits.

Matches +447222555555 | +44 7222 555 555 | (0722) 5555555 #2222
Non-Matches (+447222)555555 | +44(7222)555555 | (0722) 5555555 #22

This doesnt help with the bracketing but might give you some ideas about how to handle the number validation.

*Gets headache*

*Goes to bed*

;-)
(, Sun 7 Jun 2009, 2:55, Reply)
I got a javascript book out the libary today because I opened up on a random page and it had RegEx things.
I fucking hate regex.
(, Sat 6 Jun 2009, 23:04, Reply)
Hmm, tricky one
As above, you need to handle different lenth area codes and the possibility of a dash, space or not between the area code and the number...

Also, you need to drop the leading 0 to put the 44 on the start.

So maybe something a bit like this...(Using a php base for the pseudo code)

$cleannum = preg_replace('/0?([0-9]{4,5})[^0-9]?([0-9]{6,7})/', '0044\1\2', $num)

HTH.

Edit: Sorry, didn't spot it had been solved a lot more completely by that HUUUGE monster above!
(, Sun 7 Jun 2009, 14:38, Reply)

« Go Back | Reply To This »

Pages: Latest, 836, 835, 834, 833, 832, ... 1