Question Splitting up Mobile Phone Number

Cecil

19+ years progress programming and still learning.
I need to create a function which will split up a mobile phone number into three parts 'International Dial Code', 'Area Code' and ''Mobile Number'

Current;y on the database person contact numbers are stored in a single field and variaty of formats.

i.e.
+64212345645 or 0064212345645 or 0212345645. or even +64 (0) 21 2345 645

TestCase:
INPUT "+64212345645" would retrun "64", "021", "2345645"

INPUT "0064212345645 " would retrun "64", "021", "2345645"

INPUT "0212345645" would retrun "64", "021", "2345645"

Has anybody done this?
 

Osborne

Active Member
If you can use .NET Regex that may offer a solution although I do not know how practical it is. Not only would you possibly have to build the Regex Replace parameters depending on the length of the number, you may need to add or remove leading zeros to the number.

A simple Regex example with different options that hopefully helps:
Code:
MESSAGE System.Text.RegularExpressions.Regex:Replace("+64212345645", "^\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*$", "($1$2) $3$4$5-$6$7$8$9$10$11") SKIP
        System.Text.RegularExpressions.Regex:Replace("+64212345645", "^\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*$", "($1$2) 021-$5$6$7$8$9$10$11") SKIP
        System.Text.RegularExpressions.Regex:Replace("64212345645", "^\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*$", "($1$2) 021-$5$6$7$8$9$10$11") SKIP /* Leading zeros removed */
        System.Text.RegularExpressions.Regex:Replace("0212345645", "^\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*$", "(64) 021-$5$6$7$8$9$10") SKIP
        System.Text.RegularExpressions.Regex:Replace("00212345645", "^\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*$", "(64) 021-$5$6$7$8$9$10$11")  /* Leading zero added */
        VIEW-AS ALERT-BOX.
 

Cecil

19+ years progress programming and still learning.
nternational Dial Code
If you can use .NET Regex that may offer a solution although I do not know how practical it is. Not only would you possibly have to build the Regex Replace parameters depending on the length of the number, you may need to add or remove leading zeros to the number.

A simple Regex example with different options that hopefully helps:
Code:
MESSAGE System.Text.RegularExpressions.Regex:Replace("+64212345645", "^\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*$", "($1$2) $3$4$5-$6$7$8$9$10$11") SKIP
        System.Text.RegularExpressions.Regex:Replace("+64212345645", "^\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*$", "($1$2) 021-$5$6$7$8$9$10$11") SKIP
        System.Text.RegularExpressions.Regex:Replace("64212345645", "^\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*$", "($1$2) 021-$5$6$7$8$9$10$11") SKIP /* Leading zeros removed */
        System.Text.RegularExpressions.Regex:Replace("0212345645", "^\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*$", "(64) 021-$5$6$7$8$9$10") SKIP
        System.Text.RegularExpressions.Regex:Replace("00212345645", "^\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*$", "(64) 021-$5$6$7$8$9$10$11")  /* Leading zero added */
        VIEW-AS ALERT-BOX.

Thanks for that, may be a bit complicated to use. BUT I was able to use your code example to validate an email address, so that was very handy:

Code:
MESSAGE System.Text.RegularExpressions.Regex:IsMatch ("james@bigcorp.co,",  "([a-zA-Z0-9._%+\-]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]+)" ).
 
Top